Ticket #4305: duplicate_fieldnames.diff

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

patch + doc + tests

  • E:/Software/workspace/django-newforms-admin/django/contrib/admin/options.py

     
    148148        # doesn't work on ModelAdmin classes yet.
    149149        if self.fieldsets and self.fields:
    150150            raise ImproperlyConfigured('Both fieldsets and fields is specified for %s.' % self.model)
     151        # Detect potential duplicate fields in fieldsets declaration. Raise an exception if a duplicate is found.
     152        if self.fieldsets:
     153            flattened_fieldsets = flatten_fieldsets(self.fieldsets)
     154            if len(flattened_fieldsets) > len(sets.Set(flattened_fieldsets)):
     155                raise ImproperlyConfigured('There are duplicate field(s) in the fieldsets declaration for %s' % self.model)
    151156
    152157    def formfield_for_dbfield(self, db_field, **kwargs):
    153158        """
  • E:/Software/workspace/django-newforms-admin/tests/regressiontests/modeladmin/models.py

     
    103103['name']
    104104
    105105
     106
     107If there are duplicate fields in the fieldsets declaration, an exception
     108should be raised.
     109>>> class BandAdmin(ModelAdmin):
     110...     fieldsets = [(None, {'fields': ['name', 'name']})]
     111
     112>>> ma = BandAdmin(Band, site)
     113Traceback (most recent call last):
     114...
     115ImproperlyConfigured: There are duplicate field(s) in the fieldsets declaration for <class 'regressiontests.modeladmin.models.Band'>
     116
     117
     118
    106119If we specify a form, it should use it allowing custom validation to work
    107120properly. This won't, however, break any of the admin widgets or media.
    108121
  • E:/Software/workspace/django-newforms-admin/AUTHORS

     
    290290    peter@mymart.com
    291291    pgross@thoughtworks.com
    292292    phaedo <http://phaedo.cx/>
     293    Julien Phalip <http://www.julienphalip.com>
    293294    phil@produxion.net
    294295    phil.h.smith@gmail.com
    295296    Gustavo Picon
  • E:/Software/workspace/django-newforms-admin/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