Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.0.X] Fixed #9039 -- Don't perform unique checks on NULL values, si…

…nce NULL != NULL in SQL.

Backport of [9239] from trunk.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@9240 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit bd60c52c3c17b84f97f33db87caf8f49d715040b 1 parent 663a284
Karen Tracey authored October 21, 2008
21  django/forms/models.py
@@ -216,34 +216,31 @@ def clean(self):
216 216
     def validate_unique(self):
217 217
         from django.db.models.fields import FieldDoesNotExist
218 218
 
219  
-        # Gather a list of checks to perform. Since this is a ModelForm, some
220  
-        # fields may have been excluded; we can't perform a unique check on a
221  
-        # form that is missing fields involved in that check.
  219
+        # Gather a list of checks to perform. We only perform unique checks 
  220
+        # for fields present and not None in cleaned_data.  Since this is a 
  221
+        # ModelForm, some fields may have been excluded; we can't perform a unique 
  222
+        # check on a form that is missing fields involved in that check.  It also does
  223
+        # not make sense to check data that didn't validate, and since NULL does not 
  224
+        # equal NULL in SQL we should not do any unique checking for NULL values.
222 225
         unique_checks = []
223 226
         for check in self.instance._meta.unique_together[:]:
224  
-            fields_on_form = [field for field in check if field in self.fields]
  227
+            fields_on_form = [field for field in check if field in self.cleaned_data and not self.cleaned_data[field] is None]
225 228
             if len(fields_on_form) == len(check):
226 229
                 unique_checks.append(check)
227 230
 
228 231
         form_errors = []
229 232
 
230 233
         # Gather a list of checks for fields declared as unique and add them to
231  
-        # the list of checks. Again, skip fields not on the form.
  234
+        # the list of checks. Again, skip empty fields and any that did not validate.
232 235
         for name, field in self.fields.items():
233 236
             try:
234 237
                 f = self.instance._meta.get_field_by_name(name)[0]
235 238
             except FieldDoesNotExist:
236 239
                 # This is an extra field that's not on the ModelForm, ignore it
237 240
                 continue
238  
-            # MySQL can't handle ... WHERE pk IS NULL, so make sure we
239  
-            # don't generate queries of that form.
240  
-            is_null_pk = f.primary_key and self.cleaned_data[name] is None
241  
-            if name in self.cleaned_data and f.unique and not is_null_pk:
  241
+            if f.unique and name in self.cleaned_data and not self.cleaned_data[name] is None:
242 242
                 unique_checks.append((name,))
243 243
 
244  
-        # Don't run unique checks on fields that already have an error.
245  
-        unique_checks = [check for check in unique_checks if not [x in self._errors for x in check if x in self._errors]]
246  
-
247 244
         bad_fields = set()
248 245
         for unique_check in unique_checks:
249 246
             # Try to look up an existing object with the same values as this
36  tests/modeltests/model_forms/models.py
@@ -145,7 +145,15 @@ class Inventory(models.Model):
145 145
 
146 146
    def __unicode__(self):
147 147
       return self.name
148  
-      
  148
+
  149
+class Book(models.Model):
  150
+    title = models.CharField(max_length=40)
  151
+    author = models.ForeignKey(Writer, blank=True, null=True)
  152
+    special_id = models.IntegerField(blank=True, null=True, unique=True)
  153
+    
  154
+    class Meta:
  155
+        unique_together = ('title', 'author')
  156
+
149 157
 __test__ = {'API_TESTS': """
150 158
 >>> from django import forms
151 159
 >>> from django.forms.models import ModelForm, model_to_dict
@@ -1201,6 +1209,32 @@ def __unicode__(self):
1201 1209
 >>> form.is_valid()
1202 1210
 True
1203 1211
 
  1212
+# Unique & unique together with null values
  1213
+>>> class BookForm(ModelForm): 
  1214
+...     class Meta: 
  1215
+...        model = Book
  1216
+>>> w = Writer.objects.get(name='Mike Royko')
  1217
+>>> form = BookForm({'title': 'I May Be Wrong But I Doubt It', 'author' : w.pk})
  1218
+>>> form.is_valid()
  1219
+True
  1220
+>>> form.save()
  1221
+<Book: Book object>
  1222
+>>> form = BookForm({'title': 'I May Be Wrong But I Doubt It', 'author' : w.pk})
  1223
+>>> form.is_valid()
  1224
+False
  1225
+>>> form._errors
  1226
+{'__all__': [u'Book with this Title and Author already exists.']}
  1227
+>>> form = BookForm({'title': 'I May Be Wrong But I Doubt It'})
  1228
+>>> form.is_valid()
  1229
+True
  1230
+>>> form.save()
  1231
+<Book: Book object>
  1232
+>>> form = BookForm({'title': 'I May Be Wrong But I Doubt It'})
  1233
+>>> form.is_valid()
  1234
+True
  1235
+>>> form.save()
  1236
+<Book: Book object>
  1237
+
1204 1238
 # Choices on CharField and IntegerField
1205 1239
 >>> class ArticleForm(ModelForm):
1206 1240
 ...     class Meta:

0 notes on commit bd60c52

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