Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.2.X] Changed unique validation in model formsets to ignore None va…

…lues, not just omit them. Thanks claudep!

Backport of r14193 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14194 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 18a605d6ad1041d413f59876bef62b6f9c275ea2 1 parent 4ed1067
Honza Král authored
7  django/forms/models.py
@@ -516,10 +516,9 @@ def validate_unique(self):
516 516
                 # it's already invalid
517 517
                 if not hasattr(form, "cleaned_data"):
518 518
                     continue
519  
-                # get each of the fields for which we have data on this form
520  
-                if [f for f in unique_check if f in form.cleaned_data and form.cleaned_data[f] is not None]:
521  
-                    # get the data itself
522  
-                    row_data = tuple([form.cleaned_data[field] for field in unique_check])
  519
+                # get data for each field of each of unique_check
  520
+                row_data = tuple([form.cleaned_data[field] for field in unique_check if field in form.cleaned_data])
  521
+                if row_data and not None in row_data:
523 522
                     # if we've aready seen it then we have a uniqueness failure
524 523
                     if row_data in seen_data:
525 524
                         # poke error messages into the right places and mark
37  tests/modeltests/model_formsets/models.py
@@ -35,6 +35,23 @@ class BookWithCustomPK(models.Model):
35 35
     def __unicode__(self):
36 36
         return u'%s: %s' % (self.my_pk, self.title)
37 37
 
  38
+class Editor(models.Model):
  39
+    name = models.CharField(max_length=100)
  40
+
  41
+class BookWithOptionalAltEditor(models.Model):
  42
+    author = models.ForeignKey(Author)
  43
+    # Optional secondary author
  44
+    alt_editor = models.ForeignKey(Editor, blank=True, null=True)
  45
+    title = models.CharField(max_length=100)
  46
+
  47
+    class Meta:
  48
+        unique_together = (
  49
+            ('author', 'title', 'alt_editor'),
  50
+        )
  51
+
  52
+    def __unicode__(self):
  53
+        return self.title
  54
+
38 55
 class AlternateBook(Book):
39 56
     notes = models.CharField(max_length=100)
40 57
 
@@ -648,6 +665,26 @@ def __unicode__(self):
648 665
 >>> formset.save()
649 666
 [<AlternateBook: Flowers of Evil - English translation of Les Fleurs du Mal>]
650 667
 
  668
+Test inline formsets where the inline-edited object has a unique_together constraint with a nullable member
  669
+
  670
+>>> AuthorBooksFormSet4 = inlineformset_factory(Author, BookWithOptionalAltEditor, can_delete=False, extra=2)
  671
+
  672
+>>> data = {
  673
+...     'bookwithoptionalalteditor_set-TOTAL_FORMS': '2', # the number of forms rendered
  674
+...     'bookwithoptionalalteditor_set-INITIAL_FORMS': '0', # the number of forms with initial data
  675
+...     'bookwithoptionalalteditor_set-MAX_NUM_FORMS': '', # the max number of forms
  676
+...     'bookwithoptionalalteditor_set-0-author': '1',
  677
+...     'bookwithoptionalalteditor_set-0-title': 'Les Fleurs du Mal',
  678
+...     'bookwithoptionalalteditor_set-1-author': '1',
  679
+...     'bookwithoptionalalteditor_set-1-title': 'Les Fleurs du Mal',
  680
+... }
  681
+>>> formset = AuthorBooksFormSet4(data, instance=author)
  682
+>>> formset.is_valid()
  683
+True
  684
+
  685
+>>> formset.save()
  686
+[<BookWithOptionalAltEditor: Les Fleurs du Mal>, <BookWithOptionalAltEditor: Les Fleurs du Mal>]
  687
+
651 688
 
652 689
 # ModelForm with a custom save method in an inline formset ###################
653 690
 

0 notes on commit 18a605d

Please sign in to comment.
Something went wrong with that request. Please try again.