Fixed #7885 -- Prevent duplicates in fields and fieldsets declarations. Thanks julien and wamberg for the patches.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8173 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Brian Rosner 2008-08-01 18:55:43 +00:00
parent 64f5ab67a5
commit be17a801f6
2 changed files with 24 additions and 1 deletions

View File

@ -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 _validate_base(cls, model):
_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 _validate_base(cls, model):
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 File

@ -346,6 +346,20 @@ Traceback (most recent call last):
...
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):