summaryrefslogtreecommitdiff
path: root/django/forms/models.py
diff options
context:
space:
mode:
authorKaren Tracey <kmtracey@gmail.com>2008-10-21 14:07:36 +0000
committerKaren Tracey <kmtracey@gmail.com>2008-10-21 14:07:36 +0000
commitbd60c52c3c17b84f97f33db87caf8f49d715040b (patch)
treecdbfe123ed2abd78db6e6c06c88fec39757123fc /django/forms/models.py
parent663a2848170e97500a3ab86dc5d7413aaa93fefe (diff)
[1.0.X] Fixed #9039 -- Don't perform unique checks on NULL values, since 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
Diffstat (limited to 'django/forms/models.py')
-rw-r--r--django/forms/models.py21
1 files changed, 9 insertions, 12 deletions
diff --git a/django/forms/models.py b/django/forms/models.py
index acfa9cec3e..180aec60db 100644
--- a/django/forms/models.py
+++ b/django/forms/models.py
@@ -216,34 +216,31 @@ class BaseModelForm(BaseForm):
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