From 23e1b59cf2ad6a75637dd0273973e657e48e317e Mon Sep 17 00:00:00 2001 From: Loic Bistuer Date: Wed, 17 Apr 2013 01:49:22 +0700 Subject: [PATCH] Fixed #18681 -- BaseModelAdmin.get_form and InlineModelAdmin.get_formset no longer bypass get_fieldsets Thanks msopacua for the report. --- django/contrib/admin/options.py | 16 ++++++++-------- tests/modeladmin/tests.py | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index a0a82f32ff..97f82cbb59 100644 --- a/django/contrib/admin/options.py +++ b/django/contrib/admin/options.py @@ -498,7 +498,7 @@ class ModelAdmin(BaseModelAdmin): "Hook for specifying fieldsets for the add form." if self.declared_fieldsets: return self.declared_fieldsets - form = self.get_form(request, obj) + form = self.get_form(request, obj, fields=None) fields = list(form.base_fields) + list(self.get_readonly_fields(request, obj)) return [(None, {'fields': fields})] @@ -507,10 +507,10 @@ class ModelAdmin(BaseModelAdmin): Returns a Form class for use in the admin add view. This is used by add_view and change_view. """ - if self.declared_fieldsets: - fields = flatten_fieldsets(self.declared_fieldsets) + if 'fields' in kwargs: + fields = kwargs.pop('fields') else: - fields = None + fields = flatten_fieldsets(self.get_fieldsets(request, obj)) if self.exclude is None: exclude = [] else: @@ -1518,10 +1518,10 @@ class InlineModelAdmin(BaseModelAdmin): def get_formset(self, request, obj=None, **kwargs): """Returns a BaseInlineFormSet class for use in admin add/change views.""" - if self.declared_fieldsets: - fields = flatten_fieldsets(self.declared_fieldsets) + if 'fields' in kwargs: + fields = kwargs.pop('fields') else: - fields = None + fields = flatten_fieldsets(self.get_fieldsets(request, obj)) if self.exclude is None: exclude = [] else: @@ -1593,7 +1593,7 @@ class InlineModelAdmin(BaseModelAdmin): def get_fieldsets(self, request, obj=None): if self.declared_fieldsets: return self.declared_fieldsets - form = self.get_formset(request, obj).form + form = self.get_formset(request, obj, fields=None).form fields = list(form.base_fields) + list(self.get_readonly_fields(request, obj)) return [(None, {'fields': fields})] diff --git a/tests/modeladmin/tests.py b/tests/modeladmin/tests.py index f89f1c20ec..73e88024cd 100644 --- a/tests/modeladmin/tests.py +++ b/tests/modeladmin/tests.py @@ -64,6 +64,30 @@ class ModelAdminTests(TestCase): self.assertEqual(ma.get_fieldsets(request, self.band), [(None, {'fields': ['name', 'bio', 'sign_date']})]) + def test_get_fieldsets(self): + # Test that get_fieldsets is called when figuring out form fields. + # Refs #18681. + + class BandAdmin(ModelAdmin): + def get_fieldsets(self, request, obj=None): + return [(None, {'fields': ['name', 'bio']})] + + ma = BandAdmin(Band, self.site) + form = ma.get_form(None) + self.assertEqual(form._meta.fields, ['name', 'bio']) + + class InlineBandAdmin(TabularInline): + model = Concert + fk_name = 'main_band' + can_delete = False + + def get_fieldsets(self, request, obj=None): + return [(None, {'fields': ['day', 'transport']})] + + ma = InlineBandAdmin(Band, self.site) + form = ma.get_formset(None).form + self.assertEqual(form._meta.fields, ['day', 'transport']) + def test_field_arguments(self): # If we specify the fields argument, fieldsets_add and fielsets_change should # just stick the fields into a formsets structure and return it.