Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
@brosner brosner authored
View
11 django/contrib/admin/validation.py
@@ -1,3 +1,7 @@
+try:
+ set
+except NameError:
+ from sets import Set as set # Python 2.3 fallback
from django.core.exceptions import ImproperlyConfigured
from django.db import models
@@ -165,6 +169,8 @@ def _check_form_field_existsw(label, field):
_check_form_field_existsw('fields', field)
if cls.fieldsets:
raise ImproperlyConfigured('Both fieldsets and fields are specified in %s.' % cls.__name__)
+ if len(cls.fields) > len(set(cls.fields)):
+ raise ImproperlyConfigured('There are duplicate field(s) in %s.fields' % cls.__name__)
# fieldsets
if cls.fieldsets: # default value is None
@@ -179,7 +185,10 @@ def _check_form_field_existsw(label, field):
raise ImproperlyConfigured("`fields` key is required in "
"%s.fieldsets[%d][1] field options dict."
% (cls.__name__, idx))
- for field in flatten_fieldsets(cls.fieldsets):
+ flattened_fieldsets = flatten_fieldsets(cls.fieldsets)
+ if len(flattened_fieldsets) > len(set(flattened_fieldsets)):
+ raise ImproperlyConfigured('There are duplicate field(s) in %s.fieldsets' % cls.__name__)
+ for field in flattened_fieldsets:
_check_form_field_existsw("fieldsets[%d][1]['fields']" % idx, field)
# form
View
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
...
ImproperlyConfigured: Both fieldsets and fields are specified in ValidationTestModelAdmin.
+>>> class ValidationTestModelAdmin(ModelAdmin):
+... fieldsets = [(None, {'fields': ['name', 'name']})]
+>>> validate(ValidationTestModelAdmin, ValidationTestModel)
+Traceback (most recent call last):
+...
+ImproperlyConfigured: There are duplicate field(s) in ValidationTestModelAdmin.fieldsets
+
+>>> class ValidationTestModelAdmin(ModelAdmin):
+... fields = ["name", "name"]
+>>> validate(ValidationTestModelAdmin, ValidationTestModel)
+Traceback (most recent call last):
+...
+ImproperlyConfigured: There are duplicate field(s) in ValidationTestModelAdmin.fields
+
# form
>>> class FakeForm(object):

0 comments on commit be17a80

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