Ticket #8999: 8999.getform-exclude-kwarg.diff

File 8999.getform-exclude-kwarg.diff, 3.3 KB (added by Julien Phalip, 13 years ago)
  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index c603210..5d8006f 100644
    a b class ModelAdmin(BaseModelAdmin):  
    426426            exclude = []
    427427        else:
    428428            exclude = list(self.exclude)
    429         exclude.extend(kwargs.get("exclude", []))
    430429        exclude.extend(self.get_readonly_fields(request, obj))
    431430        # if exclude is an empty list we pass None to be consistant with the
    432431        # default on modelform_factory
    class InlineModelAdmin(BaseModelAdmin):  
    13281327            exclude = []
    13291328        else:
    13301329            exclude = list(self.exclude)
    1331         exclude.extend(kwargs.get("exclude", []))
    13321330        exclude.extend(self.get_readonly_fields(request, obj))
    13331331        # if exclude is an empty list we use None, since that's the actual
    13341332        # default
  • tests/regressiontests/modeladmin/tests.py

    diff --git a/tests/regressiontests/modeladmin/tests.py b/tests/regressiontests/modeladmin/tests.py
    index 530b476..e58b78c 100644
    a b class ModelAdminTests(TestCase):  
    141141            type(ma.get_form(request).base_fields['sign_date'].widget),
    142142            AdminDateWidget)
    143143
     144    def test_getform_exclude_kwarg_override(self):
     145        """
     146        Ensure that the `exclude` kwarg passed to `ModelAdmin.get_form()`
     147        or `InlineModelAdmin.get_formset()` overrides all other declarations.
     148        Refs #8999.
     149        """
     150       
     151        # First with `ModelAdmin.get_form()`.
     152       
     153        class AdminBandForm(forms.ModelForm):
     154
     155            class Meta:
     156                model = Band
     157                exclude = ['name']
     158
     159        class BandAdmin(ModelAdmin):
     160            exclude = ['sign_date',]
     161            form = AdminBandForm
     162           
     163            def get_form(self, request, obj=None, **kwargs):
     164                kwargs['exclude'] = ['bio']
     165                return super(BandAdmin, self).get_form(request, obj, **kwargs)
     166       
     167        ma = BandAdmin(Band, self.site)
     168        self.assertEqual(ma.get_form(request).base_fields.keys(),
     169            ['name', 'sign_date',])
     170       
     171        # Then, same thing with `InlineModelAdmin.get_formset()`.
     172       
     173        class AdminConcertForm(forms.ModelForm):
     174
     175            class Meta:
     176                model = Concert
     177                exclude = ['day']
     178       
     179        class ConcertInline(TabularInline):
     180            exclude = ['transport']
     181            form = AdminConcertForm
     182            fk_name = 'main_band'
     183            model = Concert
     184
     185            def get_formset(self, request, obj=None, **kwargs):
     186                kwargs['exclude'] = ['opening_band']
     187                return super(ConcertInline, self).get_formset(request, obj, **kwargs)
     188           
     189        class BandAdmin(ModelAdmin):
     190            inlines = [
     191                ConcertInline
     192            ]
     193
     194        ma = BandAdmin(Band, self.site)
     195        self.assertEqual(
     196            list(ma.get_formsets(request))[0]().forms[0].fields.keys(),
     197            ['main_band', 'day', 'transport', 'id', 'DELETE',])
     198       
    144199    def test_queryset_override(self):
    145200        # If we need to override the queryset of a ModelChoiceField in our custom form
    146201        # make sure that RelatedFieldWidgetWrapper doesn't mess that up.
Back to Top