Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

Fixed #10069 -- Fixed the model form unique validation code to not pr…

…oceed with using, for example, RelatedObjects returned by get_field_by_name as though they were model Fields.

git-svn-id: bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 9c6efb2 commit 795c229ae940b9bc543aacdf6dd0a30ffcc62b3c @kmtracey kmtracey committed
Showing with 18 additions and 1 deletion.
  1. +7 −1 django/forms/
  2. +11 −0 tests/modeltests/model_forms/
8 django/forms/
@@ -224,7 +224,7 @@ def clean(self):
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 @@ def validate_unique(self):
except FieldDoesNotExist:
# This is an extra field that's not on the ModelForm, ignore it
+ 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:
11 tests/modeltests/model_forms/
@@ -193,6 +193,17 @@ def __unicode__(self):
>>> 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()
Replacing a field.

0 comments on commit 795c229

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