summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKaren Tracey <kmtracey@gmail.com>2009-01-19 19:37:31 +0000
committerKaren Tracey <kmtracey@gmail.com>2009-01-19 19:37:31 +0000
commit795c229ae940b9bc543aacdf6dd0a30ffcc62b3c (patch)
treefe4e4a8d41ab3fd2084c0468a119c8be75ae92a2
parent9c6efb2eeaca11c7e65ce3b63a09f2d1e0b247c3 (diff)
Fixed #10069 -- Fixed the model form unique validation code to not proceed with using, for example, RelatedObjects returned by get_field_by_name as though they were model Fields.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@9777 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/forms/models.py8
-rw-r--r--tests/modeltests/model_forms/models.py11
2 files changed, 18 insertions, 1 deletions
diff --git a/django/forms/models.py b/django/forms/models.py
index 03ad10a4ea..01bd912063 100644
--- a/django/forms/models.py
+++ b/django/forms/models.py
@@ -224,7 +224,7 @@ class BaseModelForm(BaseForm):
return self.cleaned_data
def validate_unique(self):
- from django.db.models.fields import FieldDoesNotExist
+ from django.db.models.fields import FieldDoesNotExist, Field as ModelField
# 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
@@ -248,6 +248,12 @@ class BaseModelForm(BaseForm):
except FieldDoesNotExist:
# This is an extra field that's not on the ModelForm, ignore it
continue
+ if not isinstance(f, ModelField):
+ # This is an extra field that happens to have a name that matches,
+ # for example, a related object accessor for this model. So
+ # get_field_by_name found it, but it is not a Field so do not proceed
+ # to use it as if it were.
+ continue
if f.unique and self.cleaned_data.get(name) is not None:
unique_checks.append((name,))
diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py
index 0489ea81d8..46d26c5cdc 100644
--- a/tests/modeltests/model_forms/models.py
+++ b/tests/modeltests/model_forms/models.py
@@ -193,6 +193,17 @@ Extra fields.
>>> CategoryForm.base_fields.keys()
['name', 'slug', 'url', 'some_extra_field']
+Extra field that has a name collision with a related object accessor.
+
+>>> class WriterForm(ModelForm):
+... book = forms.CharField(required=False)
+...
+... class Meta:
+... model = Writer
+
+>>> wf = WriterForm({'name': 'Richard Lockridge'})
+>>> wf.is_valid()
+True
Replacing a field.