Author: kmtracey
Date: 2008-10-21 09:07:36 -0500 (Tue, 21 Oct 2008)
New Revision: 9240

Modified:
   django/branches/releases/1.0.X/
   django/branches/releases/1.0.X/django/forms/models.py
   django/branches/releases/1.0.X/tests/modeltests/model_forms/models.py
Log:
[1.0.X] Fixed #9039 -- Don't perform unique checks on NULL values, since NULL 
!= NULL in SQL.

Backport of [9239] from trunk.



Property changes on: django/branches/releases/1.0.X
___________________________________________________________________
Name: svnmerge-integrated
   - 
/django/trunk:1-9097,9099-9102,9104-9109,9111,9113-9144,9146-9151,9153-9156,9158-9159,9161-9187,9189-9233,9235
   + 
/django/trunk:1-9097,9099-9102,9104-9109,9111,9113-9144,9146-9151,9153-9156,9158-9159,9161-9187,9189-9239

Modified: django/branches/releases/1.0.X/django/forms/models.py
===================================================================
--- django/branches/releases/1.0.X/django/forms/models.py       2008-10-21 
14:04:24 UTC (rev 9239)
+++ django/branches/releases/1.0.X/django/forms/models.py       2008-10-21 
14:07:36 UTC (rev 9240)
@@ -216,34 +216,31 @@
     def validate_unique(self):
         from django.db.models.fields import FieldDoesNotExist
 
-        # Gather a list of checks to perform. Since this is a ModelForm, some
-        # fields may have been excluded; we can't perform a unique check on a
-        # form that is missing fields involved in that check.
+        # Gather a list of checks to perform. We only perform unique checks 
+        # for fields present and not None in cleaned_data.  Since this is a 
+        # ModelForm, some fields may have been excluded; we can't perform a 
unique 
+        # check on a form that is missing fields involved in that check.  It 
also does
+        # not make sense to check data that didn't validate, and since NULL 
does not 
+        # equal NULL in SQL we should not do any unique checking for NULL 
values.
         unique_checks = []
         for check in self.instance._meta.unique_together[:]:
-            fields_on_form = [field for field in check if field in self.fields]
+            fields_on_form = [field for field in check if field in 
self.cleaned_data and not self.cleaned_data[field] is None]
             if len(fields_on_form) == len(check):
                 unique_checks.append(check)
 
         form_errors = []
 
         # Gather a list of checks for fields declared as unique and add them to
-        # the list of checks. Again, skip fields not on the form.
+        # the list of checks. Again, skip empty fields and any that did not 
validate.
         for name, field in self.fields.items():
             try:
                 f = self.instance._meta.get_field_by_name(name)[0]
             except FieldDoesNotExist:
                 # This is an extra field that's not on the ModelForm, ignore it
                 continue
-            # MySQL can't handle ... WHERE pk IS NULL, so make sure we
-            # don't generate queries of that form.
-            is_null_pk = f.primary_key and self.cleaned_data[name] is None
-            if name in self.cleaned_data and f.unique and not is_null_pk:
+            if f.unique and name in self.cleaned_data and not 
self.cleaned_data[name] is None:
                 unique_checks.append((name,))
 
-        # Don't run unique checks on fields that already have an error.
-        unique_checks = [check for check in unique_checks if not [x in 
self._errors for x in check if x in self._errors]]
-
         bad_fields = set()
         for unique_check in unique_checks:
             # Try to look up an existing object with the same values as this

Modified: django/branches/releases/1.0.X/tests/modeltests/model_forms/models.py
===================================================================
--- django/branches/releases/1.0.X/tests/modeltests/model_forms/models.py       
2008-10-21 14:04:24 UTC (rev 9239)
+++ django/branches/releases/1.0.X/tests/modeltests/model_forms/models.py       
2008-10-21 14:07:36 UTC (rev 9240)
@@ -145,7 +145,15 @@
 
    def __unicode__(self):
       return self.name
-      
+
+class Book(models.Model):
+    title = models.CharField(max_length=40)
+    author = models.ForeignKey(Writer, blank=True, null=True)
+    special_id = models.IntegerField(blank=True, null=True, unique=True)
+    
+    class Meta:
+        unique_together = ('title', 'author')
+
 __test__ = {'API_TESTS': """
 >>> from django import forms
 >>> from django.forms.models import ModelForm, model_to_dict
@@ -1201,6 +1209,32 @@
 >>> form.is_valid()
 True
 
+# Unique & unique together with null values
+>>> class BookForm(ModelForm): 
+...     class Meta: 
+...        model = Book
+>>> w = Writer.objects.get(name='Mike Royko')
+>>> form = BookForm({'title': 'I May Be Wrong But I Doubt It', 'author' : 
w.pk})
+>>> form.is_valid()
+True
+>>> form.save()
+<Book: Book object>
+>>> form = BookForm({'title': 'I May Be Wrong But I Doubt It', 'author' : 
w.pk})
+>>> form.is_valid()
+False
+>>> form._errors
+{'__all__': [u'Book with this Title and Author already exists.']}
+>>> form = BookForm({'title': 'I May Be Wrong But I Doubt It'})
+>>> form.is_valid()
+True
+>>> form.save()
+<Book: Book object>
+>>> form = BookForm({'title': 'I May Be Wrong But I Doubt It'})
+>>> form.is_valid()
+True
+>>> form.save()
+<Book: Book object>
+
 # Choices on CharField and IntegerField
 >>> class ArticleForm(ModelForm):
 ...     class Meta:


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

Reply via email to