Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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: http://code.djangoproject.com/svn/django/trunk@9777 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 795c229ae940b9bc543aacdf6dd0a30ffcc62b3c 1 parent 9c6efb2
Karen Tracey authored January 19, 2009
8  django/forms/models.py
@@ -224,7 +224,7 @@ def clean(self):
224 224
         return self.cleaned_data
225 225
 
226 226
     def validate_unique(self):
227  
-        from django.db.models.fields import FieldDoesNotExist
  227
+        from django.db.models.fields import FieldDoesNotExist, Field as ModelField
228 228
 
229 229
         # Gather a list of checks to perform. We only perform unique checks
230 230
         # for fields present and not None in cleaned_data.  Since this is a
@@ -248,6 +248,12 @@ def validate_unique(self):
248 248
             except FieldDoesNotExist:
249 249
                 # This is an extra field that's not on the ModelForm, ignore it
250 250
                 continue
  251
+            if not isinstance(f, ModelField):
  252
+                # This is an extra field that happens to have a name that matches, 
  253
+                # for example, a related object accessor for this model.  So 
  254
+                # get_field_by_name found it, but it is not a Field so do not proceed
  255
+                # to use it as if it were.
  256
+                continue
251 257
             if f.unique and self.cleaned_data.get(name) is not None:
252 258
                 unique_checks.append((name,))
253 259
 
11  tests/modeltests/model_forms/models.py
@@ -193,6 +193,17 @@ def __unicode__(self):
193 193
 >>> CategoryForm.base_fields.keys()
194 194
 ['name', 'slug', 'url', 'some_extra_field']
195 195
 
  196
+Extra field that has a name collision with a related object accessor.
  197
+
  198
+>>> class WriterForm(ModelForm):
  199
+...     book = forms.CharField(required=False)
  200
+...
  201
+...     class Meta:
  202
+...         model = Writer
  203
+
  204
+>>> wf = WriterForm({'name': 'Richard Lockridge'})
  205
+>>> wf.is_valid()
  206
+True
196 207
 
197 208
 Replacing a field.
198 209
 

0 notes on commit 795c229

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