Ticket #8999: modeladmin-getform-and-getformset-exclude-fix-with-tests.diff

File modeladmin-getform-and-getformset-exclude-fix-with-tests.diff, 4.6 KB (added by Sean Legassick, 11 years ago)
  • django/contrib/admin/options.py

     
    266266        if self.exclude is None:
    267267            exclude = []
    268268        else:
    269             exclude = self.exclude
     269            exclude = list(self.exclude)
    270270        defaults = {
    271271            "form": self.form,
    272272            "fields": fields,
    273             "exclude": exclude + kwargs.get("exclude", []),
     273            "exclude": exclude + list(kwargs.pop("exclude", [])),
    274274            "formfield_callback": self.formfield_for_dbfield,
    275275        }
    276276        defaults.update(kwargs)
     
    780780        if self.exclude is None:
    781781            exclude = []
    782782        else:
    783             exclude = self.exclude
     783            exclude = list(self.exclude)
    784784        defaults = {
    785785            "form": self.form,
    786786            "formset": self.formset,
    787787            "fk_name": self.fk_name,
    788788            "fields": fields,
    789             "exclude": exclude + kwargs.get("exclude", []),
     789            "exclude": exclude + list(kwargs.pop("exclude", [])),
    790790            "formfield_callback": self.formfield_for_dbfield,
    791791            "extra": self.extra,
    792792            "max_num": self.max_num,
  • tests/regressiontests/modeladmin/models.py

     
    3737
    3838__test__ = {'API_TESTS': """
    3939
    40 >>> from django.contrib.admin.options import ModelAdmin, HORIZONTAL, VERTICAL
     40>>> from django.contrib.admin.options import ModelAdmin, StackedInline, HORIZONTAL, VERTICAL
    4141>>> from django.contrib.admin.sites import AdminSite
    4242
    4343None of the following tests really depend on the content of the request, so
     
    123123>>> ma = BandAdmin(Band, site)
    124124>>> ma.get_form(request).base_fields.keys()
    125125['name', 'sign_date']
    126  
     126
     127# Using a tuple for `exclude`.
     128
     129>>> class BandAdmin(ModelAdmin):
     130...     exclude = ('bio',)
     131>>> ma = BandAdmin(Band, site)
     132>>> ma.get_form(request).base_fields.keys()
     133['name', 'sign_date']
     134
     135# Combining `exclude` tuple on the class and `exclude` list as get_form parameter.
     136
     137>>> class BandAdmin(ModelAdmin):
     138...     exclude = ('bio',)
     139>>> ma = BandAdmin(Band, site)
     140>>> ma.get_form(request, exclude=['sign_date']).base_fields.keys()
     141['name']
     142
     143# Combining `exclude` tuple on the class and `exclude` tuple as get_form parameter.
     144
     145>>> class BandAdmin(ModelAdmin):
     146...     exclude = ('bio',)
     147>>> ma = BandAdmin(Band, site)
     148>>> ma.get_form(request, exclude=('sign_date',)).base_fields.keys()
     149['name']
     150
    127151# Using `fields` and `exclude`.
    128152
    129153>>> class BandAdmin(ModelAdmin):
     
    133157>>> ma.get_form(request).base_fields.keys()
    134158['name']
    135159
     160# Using `exclude` on an inline admin.
     161
     162>>> class ConcertAdmin(StackedInline):
     163...     model = Concert
     164...     fk_name = 'main_band'
     165...     exclude = ['transport']
     166>>> ma = ConcertAdmin(Band, site)
     167>>> ma.get_formset(request).form.base_fields.keys()
     168['opening_band', 'day']
     169
     170# Using a tuple for `exclude` on an inline admin.
     171
     172>>> class ConcertAdmin(StackedInline):
     173...     model = Concert
     174...     fk_name = 'main_band'
     175...     exclude = ('transport',)
     176>>> ma = ConcertAdmin(Band, site)
     177>>> ma.get_formset(request).form.base_fields.keys()
     178['opening_band', 'day']
     179
     180# Combining `exclude` tuple on the class and `exclude` list as get_form parameter on inline.
     181
     182>>> class ConcertAdmin(StackedInline):
     183...     model = Concert
     184...     fk_name = 'main_band'
     185...     exclude = ['transport']
     186>>> ma = ConcertAdmin(Band, site)
     187>>> ma.get_formset(request, exclude=['opening_band']).form.base_fields.keys()
     188['day']
     189
     190# Combining `exclude` tuple on the class and `exclude` tuple as get_form parameter on inline.
     191
     192>>> class ConcertAdmin(StackedInline):
     193...     model = Concert
     194...     fk_name = 'main_band'
     195...     exclude = ['transport']
     196>>> ma = ConcertAdmin(Band, site)
     197>>> ma.get_formset(request, exclude=('opening_band',)).form.base_fields.keys()
     198['day']
     199
     200# Using `fields` and `exclude` on inline.
     201
     202>>> class ConcertAdmin(StackedInline):
     203...     model = Concert
     204...     fk_name = 'main_band'
     205...     fields = ['opening_band', 'transport']
     206...     exclude = ['transport']
     207>>> ma = ConcertAdmin(Band, site)
     208>>> ma.get_formset(request).form.base_fields.keys()
     209['opening_band']
     210
    136211If we specify a form, it should use it allowing custom validation to work
    137212properly. This won't, however, break any of the admin widgets or media.
    138213
Back to Top