Author: Honza_Kral
Date: 2010-10-12 19:33:10 -0500 (Tue, 12 Oct 2010)
New Revision: 14194

Modified:
   django/branches/releases/1.2.X/django/forms/models.py
   django/branches/releases/1.2.X/tests/modeltests/model_formsets/models.py
Log:
[1.2.X] Changed unique validation in model formsets to ignore None values, not 
just omit them. Thanks claudep!

Backport of r14193 from trunk.

Modified: django/branches/releases/1.2.X/django/forms/models.py
===================================================================
--- django/branches/releases/1.2.X/django/forms/models.py       2010-10-13 
00:30:02 UTC (rev 14193)
+++ django/branches/releases/1.2.X/django/forms/models.py       2010-10-13 
00:33:10 UTC (rev 14194)
@@ -516,10 +516,9 @@
                 # it's already invalid
                 if not hasattr(form, "cleaned_data"):
                     continue
-                # get each of the fields for which we have data on this form
-                if [f for f in unique_check if f in form.cleaned_data and 
form.cleaned_data[f] is not None]:
-                    # get the data itself
-                    row_data = tuple([form.cleaned_data[field] for field in 
unique_check])
+                # get data for each field of each of unique_check
+                row_data = tuple([form.cleaned_data[field] for field in 
unique_check if field in form.cleaned_data])
+                if row_data and not None in row_data:
                     # if we've aready seen it then we have a uniqueness failure
                     if row_data in seen_data:
                         # poke error messages into the right places and mark

Modified: 
django/branches/releases/1.2.X/tests/modeltests/model_formsets/models.py
===================================================================
--- django/branches/releases/1.2.X/tests/modeltests/model_formsets/models.py    
2010-10-13 00:30:02 UTC (rev 14193)
+++ django/branches/releases/1.2.X/tests/modeltests/model_formsets/models.py    
2010-10-13 00:33:10 UTC (rev 14194)
@@ -35,6 +35,23 @@
     def __unicode__(self):
         return u'%s: %s' % (self.my_pk, self.title)
 
+class Editor(models.Model):
+    name = models.CharField(max_length=100)
+
+class BookWithOptionalAltEditor(models.Model):
+    author = models.ForeignKey(Author)
+    # Optional secondary author
+    alt_editor = models.ForeignKey(Editor, blank=True, null=True)
+    title = models.CharField(max_length=100)
+
+    class Meta:
+        unique_together = (
+            ('author', 'title', 'alt_editor'),
+        )
+
+    def __unicode__(self):
+        return self.title
+
 class AlternateBook(Book):
     notes = models.CharField(max_length=100)
 
@@ -648,7 +665,27 @@
 >>> formset.save()
 [<AlternateBook: Flowers of Evil - English translation of Les Fleurs du Mal>]
 
+Test inline formsets where the inline-edited object has a unique_together 
constraint with a nullable member
 
+>>> AuthorBooksFormSet4 = inlineformset_factory(Author, 
BookWithOptionalAltEditor, can_delete=False, extra=2)
+
+>>> data = {
+...     'bookwithoptionalalteditor_set-TOTAL_FORMS': '2', # the number of 
forms rendered
+...     'bookwithoptionalalteditor_set-INITIAL_FORMS': '0', # the number of 
forms with initial data
+...     'bookwithoptionalalteditor_set-MAX_NUM_FORMS': '', # the max number of 
forms
+...     'bookwithoptionalalteditor_set-0-author': '1',
+...     'bookwithoptionalalteditor_set-0-title': 'Les Fleurs du Mal',
+...     'bookwithoptionalalteditor_set-1-author': '1',
+...     'bookwithoptionalalteditor_set-1-title': 'Les Fleurs du Mal',
+... }
+>>> formset = AuthorBooksFormSet4(data, instance=author)
+>>> formset.is_valid()
+True
+
+>>> formset.save()
+[<BookWithOptionalAltEditor: Les Fleurs du Mal>, <BookWithOptionalAltEditor: 
Les Fleurs du Mal>]
+
+
 # ModelForm with a custom save method in an inline formset ###################
 
 >>> class PoemForm(forms.ModelForm):

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-upda...@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to