Ticket #8999: modeladmin-getform-and-getformset-exclude-will-overwrite.diff

File modeladmin-getform-and-getformset-exclude-will-overwrite.diff, 2.2 KB (added by Brandon Konkle, 9 years ago)
  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index 1f8ff6d..c1a8450 100644
    a b class ModelAdmin(BaseModelAdmin): 
    352352            exclude = []
    353353        else:
    354354            exclude = list(self.exclude)
    355         exclude.extend(kwargs.get("exclude", []))
    356355        exclude.extend(self.get_readonly_fields(request, obj))
    357356        # if exclude is an empty list we pass None to be consistant with the
    358357        # default on modelform_factory
    class InlineModelAdmin(BaseModelAdmin): 
    12441243            exclude = []
    12451244        else:
    12461245            exclude = list(self.exclude)
    1247         exclude.extend(kwargs.get("exclude", []))
    12481246        exclude.extend(self.get_readonly_fields(request, obj))
    12491247        # if exclude is an empty list we use None, since that's the actual
    12501248        # default
  • tests/regressiontests/modeladmin/models.py

    diff --git a/tests/regressiontests/modeladmin/models.py b/tests/regressiontests/modeladmin/models.py
    index 36ea416..0133f35 100644
    a b displayed because you forgot to add it to fields/fielsets 
    141141>>> ma.get_form(request).base_fields.keys()
    142142['name']
    143143
     144# Passing `exclude` to get_form as a keyword argument will override the
     145# value generated by the method, disregarding the `exclude` and
     146# `readonly_fields` attributes on the ModelAdmin instance.
     147
     148>>> class BandAdmin(ModelAdmin):
     149...     fields = ['name', 'bio', 'sign_date']
     150...     exclude = ['bio']
     151...     readonly_fields = ['sign_date']
     152>>> ma = BandAdmin(Band, site)
     153>>> ma.get_form(request, exclude=['name']).base_fields.keys()
     154['bio', 'sign_date']
     155
    144156If we specify a form, it should use it allowing custom validation to work
    145157properly. This won't, however, break any of the admin widgets or media.
    146158
    blank=True for the model field. Finally, the widget should have the 
    302314>>> list(ma.get_formsets(request))[0]().forms[0].fields.keys()
    303315['extra', 'transport', 'id', 'DELETE', 'main_band']
    304316
     317# Using `exclude` on InlineModelAdmin.get_formset
     318>>> ma.inline_instances[0].get_formset(request, exclude=['transport']).form.base_fields.keys()
     319['extra', 'main_band']
     320
    305321
    306322>>> band.delete()
    307323
Back to Top