Ticket #7885: duplicate_field_declarations.diff

File duplicate_field_declarations.diff, 2.5 KB (added by Julien Phalip, 16 years ago)

Patch + tests

  • django/django/contrib/admin/validation.py

     
    1 
     1import sets
    22from django.core.exceptions import ImproperlyConfigured
    33from django.db import models
    44from django.forms.models import BaseModelForm, BaseModelFormSet, fields_for_model
     
    165165            _check_form_field_existsw('fields', field)
    166166        if cls.fieldsets:
    167167            raise ImproperlyConfigured('Both fieldsets and fields are specified in %s.' % cls.__name__)
     168        if len(cls.fields) > len(sets.Set(cls.fields)):
     169            raise ImproperlyConfigured('There are duplicate field(s) in %s.fields' % cls.__name__)
    168170
    169171    # fieldsets
    170172    if cls.fieldsets: # default value is None
     
    179181                raise ImproperlyConfigured("`fields` key is required in "
    180182                        "%s.fieldsets[%d][1] field options dict."
    181183                        % (cls.__name__, idx))
    182         for field in flatten_fieldsets(cls.fieldsets):
     184        flattened_fieldsets = flatten_fieldsets(cls.fieldsets)
     185        if len(flattened_fieldsets) > len(sets.Set(flattened_fieldsets)):
     186            raise ImproperlyConfigured('There are duplicate field(s) in %s.fieldsets' % cls.__name__)
     187        for field in flattened_fieldsets:
    183188            _check_form_field_existsw("fieldsets[%d][1]['fields']" % idx, field)
    184189
    185190    # form
  • django/tests/regressiontests/modeladmin/models.py

     
    346346...
    347347ImproperlyConfigured: Both fieldsets and fields are specified in ValidationTestModelAdmin.
    348348
     349>>> class ValidationTestModelAdmin(ModelAdmin):
     350...     fieldsets = [(None, {'fields': ['name', 'name']})]
     351>>> validate(ValidationTestModelAdmin, ValidationTestModel)
     352Traceback (most recent call last):
     353...
     354ImproperlyConfigured: There are duplicate field(s) in ValidationTestModelAdmin.fieldsets
     355
     356>>> class ValidationTestModelAdmin(ModelAdmin):
     357...     fields = ["name", "name"]
     358>>> validate(ValidationTestModelAdmin, ValidationTestModel)
     359Traceback (most recent call last):
     360...
     361ImproperlyConfigured: There are duplicate field(s) in ValidationTestModelAdmin.fields
     362
    349363# form
    350364
    351365>>> class FakeForm(object):
Back to Top