From a0ed2f9260f995b0cdf145f2802fc8123c25db65 Mon Sep 17 00:00:00 2001 From: Loic Bistuer Date: Wed, 31 Jul 2013 12:50:39 +0700 Subject: [PATCH] Fixed #18681 -- GenericInlineModelAdmin.get_formset() no longer bypasses get_fieldsets(). Refs 23e1b59 which already fixed this issue for ModelAdmin and InlineModelAdmin. --- django/contrib/contenttypes/generic.py | 6 +++--- tests/generic_inline_admin/tests.py | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/django/contrib/contenttypes/generic.py b/django/contrib/contenttypes/generic.py index 6f120f82e09..186f39068d7 100644 --- a/django/contrib/contenttypes/generic.py +++ b/django/contrib/contenttypes/generic.py @@ -465,10 +465,10 @@ class GenericInlineModelAdmin(InlineModelAdmin): formset = BaseGenericInlineFormSet def get_formset(self, request, obj=None, **kwargs): - 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: diff --git a/tests/generic_inline_admin/tests.py b/tests/generic_inline_admin/tests.py index ccf12ab4f3a..83bee2f4e02 100644 --- a/tests/generic_inline_admin/tests.py +++ b/tests/generic_inline_admin/tests.py @@ -325,3 +325,23 @@ class GenericInlineModelAdminTest(TestCase): self.assertEqual( list(list(ma.get_formsets(request))[0]().forms[0].fields), ['description', 'keywords', 'id', 'DELETE']) + + def test_get_fieldsets(self): + # Test that get_fieldsets is called when figuring out form fields. + # Refs #18681. + class MediaForm(ModelForm): + class Meta: + model = Media + fields = '__all__' + + class MediaInline(GenericTabularInline): + form = MediaForm + model = Media + can_delete = False + + def get_fieldsets(self, request, obj=None): + return [(None, {'fields': ['url', 'description']})] + + ma = MediaInline(Media, self.site) + form = ma.get_formset(None).form + self.assertEqual(form._meta.fields, ['url', 'description'])