Ticket #7885: duplicate_field_declarations_2.diff

File duplicate_field_declarations_2.diff, 2.4 KB (added by wamberg, 16 years ago)

Added python2.3 support to previous patch.

  • django/contrib/admin/validation.py

     
     1try:
     2    set
     3except NameError:
     4    from sets import Set as set   # Python 2.3 fallback
    15
    26from django.core.exceptions import ImproperlyConfigured
    37from django.db import models
     
    165169            _check_form_field_existsw('fields', field)
    166170        if cls.fieldsets:
    167171            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__)
    168174
    169175    # fieldsets
    170176    if cls.fieldsets: # default value is None
     
    179185                raise ImproperlyConfigured("`fields` key is required in "
    180186                        "%s.fieldsets[%d][1] field options dict."
    181187                        % (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:
    183192            _check_form_field_existsw("fieldsets[%d][1]['fields']" % idx, field)
    184193
    185194    # form
  • 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