From be17a801f6053fe8ef6250cc1a25f80db6956698 Mon Sep 17 00:00:00 2001 From: Brian Rosner Date: Fri, 1 Aug 2008 18:55:43 +0000 Subject: [PATCH] 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 --- django/contrib/admin/validation.py | 11 ++++++++++- tests/regressiontests/modeladmin/models.py | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/django/contrib/admin/validation.py b/django/contrib/admin/validation.py index 2c9cb8554d..a42f2eb985 100644 --- a/django/contrib/admin/validation.py +++ b/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 _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 diff --git a/tests/regressiontests/modeladmin/models.py b/tests/regressiontests/modeladmin/models.py index 6a7da7d362..0fdd9afdf1 100644 --- a/tests/regressiontests/modeladmin/models.py +++ b/tests/regressiontests/modeladmin/models.py @@ -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):