Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #7885 -- Prevent duplicates in fields and fieldsets declaration…

…s. Thanks julien and wamberg for the patches.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8173 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit be17a801f6053fe8ef6250cc1a25f80db6956698 1 parent 64f5ab6
Brian Rosner authored August 01, 2008
11  django/contrib/admin/validation.py
... ...
@@ -1,3 +1,7 @@
  1
+try:
  2
+    set
  3
+except NameError:
  4
+    from sets import Set as set   # Python 2.3 fallback
1 5
 
2 6
 from django.core.exceptions import ImproperlyConfigured
3 7
 from django.db import models
@@ -165,6 +169,8 @@ def _check_form_field_existsw(label, field):
165 169
             _check_form_field_existsw('fields', field)
166 170
         if cls.fieldsets:
167 171
             raise ImproperlyConfigured('Both fieldsets and fields are specified in %s.' % cls.__name__)
  172
+        if len(cls.fields) > len(set(cls.fields)):
  173
+            raise ImproperlyConfigured('There are duplicate field(s) in %s.fields' % cls.__name__)
168 174
 
169 175
     # fieldsets
170 176
     if cls.fieldsets: # default value is None
@@ -179,7 +185,10 @@ def _check_form_field_existsw(label, field):
179 185
                 raise ImproperlyConfigured("`fields` key is required in "
180 186
                         "%s.fieldsets[%d][1] field options dict."
181 187
                         % (cls.__name__, idx))
182  
-        for field in flatten_fieldsets(cls.fieldsets):
  188
+        flattened_fieldsets = flatten_fieldsets(cls.fieldsets)
  189
+        if len(flattened_fieldsets) > len(set(flattened_fieldsets)):
  190
+            raise ImproperlyConfigured('There are duplicate field(s) in %s.fieldsets' % cls.__name__)
  191
+        for field in flattened_fieldsets:
183 192
             _check_form_field_existsw("fieldsets[%d][1]['fields']" % idx, field)
184 193
 
185 194
     # form
14  tests/regressiontests/modeladmin/models.py
@@ -346,6 +346,20 @@ class and an AdminSite instance, so let's just go ahead and do that manually
346 346
 ...
347 347
 ImproperlyConfigured: Both fieldsets and fields are specified in ValidationTestModelAdmin.
348 348
 
  349
+>>> class ValidationTestModelAdmin(ModelAdmin): 
  350
+...     fieldsets = [(None, {'fields': ['name', 'name']})] 
  351
+>>> validate(ValidationTestModelAdmin, ValidationTestModel) 
  352
+Traceback (most recent call last): 
  353
+... 
  354
+ImproperlyConfigured: There are duplicate field(s) in ValidationTestModelAdmin.fieldsets
  355
+
  356
+>>> class ValidationTestModelAdmin(ModelAdmin):
  357
+...     fields = ["name", "name"]
  358
+>>> validate(ValidationTestModelAdmin, ValidationTestModel)
  359
+Traceback (most recent call last):
  360
+...
  361
+ImproperlyConfigured: There are duplicate field(s) in ValidationTestModelAdmin.fields
  362
+
349 363
 # form
350 364
 
351 365
 >>> class FakeForm(object):

0 notes on commit be17a80

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