Ticket #4305: 4305.duplicate_fieldnames.diff

File 4305.duplicate_fieldnames.diff, 2.9 KB (added by Julien Phalip, 16 years ago)

Updated patch so it uses the ModelAdmin's validation introduced in [7929]

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

     
    1 
     1import sets
    22from django.core.exceptions import ImproperlyConfigured
    33from django.db import models
    44from django.newforms.models import BaseModelForm, BaseInlineFormset
     
    168168                raise ImproperlyConfigured("`fields` key is required in "
    169169                        "%s.fieldsets[%d][1] field options dict."
    170170                        % (cls.__name__, idx))
    171         for field in flatten_fieldsets(cls.fieldsets):
     171        flattened_fieldsets = flatten_fieldsets(cls.fieldsets)
     172        if len(flattened_fieldsets) > len(sets.Set(flattened_fieldsets)):
     173            raise ImproperlyConfigured('There are duplicate field(s) in %s.fieldsets' % cls.__name__)
     174        for field in flattened_fieldsets:
    172175            _check_field_existsw("fieldsets[%d][1]['fields']" % idx, field)
    173176
    174177    # form
  • django/tests/regressiontests/modeladmin/models.py

     
    337337...     fieldsets = (("General", {"fields": ("name",)}),)
    338338>>> validate(ValidationTestModelAdmin, ValidationTestModel)
    339339
     340If there are duplicate fields in the fieldsets declaration, an exception
     341should be raised.
     342>>> class ValidationTestModelAdmin(ModelAdmin):
     343...     fieldsets = [(None, {'fields': ['name', 'name']})]
     344>>> validate(ValidationTestModelAdmin, ValidationTestModel)
     345Traceback (most recent call last):
     346...
     347ImproperlyConfigured: There are duplicate field(s) in ValidationTestModelAdmin.fieldsets
     348
    340349# form
    341350
    342351>>> class FakeForm(object):
  • django/docs/admin.txt

     
    102102that isn't an ``AutoField`` and has ``editable=True``, in a single fieldset,
    103103in the same order as the fields are defined in the model.
    104104
     105``fieldsets`` should not contain duplicates (a duplicate meaning that a same field
     106appears multiple times in the declaration). If a duplicate is found then an exception
     107will be raised. For example, the following would **not** be valid, as the ``pub_date``
     108field appears twice::
     109
     110    class PollAdmin(admin.ModelAdmin):
     111        fieldsets = (
     112            (None, {
     113                'fields': ('pub_date', 'question')
     114            }),
     115            ('Date information', {
     116                'fields': ('pub_date',)
     117            }),
     118    )
     119
    105120The ``field_options`` dictionary can have the following keys:
    106121
    107122``fields``
Back to Top