Fixed #18681 -- BaseModelAdmin.get_form and InlineModelAdmin.get_formset no longer bypass get_fieldsets

Thanks msopacua for the report.
This commit is contained in:
Loic Bistuer 2013-04-17 01:49:22 +07:00 committed by Tim Graham
parent f10e9af227
commit 23e1b59cf2
2 changed files with 32 additions and 8 deletions

View File

@ -498,7 +498,7 @@ class ModelAdmin(BaseModelAdmin):
"Hook for specifying fieldsets for the add form." "Hook for specifying fieldsets for the add form."
if self.declared_fieldsets: if self.declared_fieldsets:
return 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)) fields = list(form.base_fields) + list(self.get_readonly_fields(request, obj))
return [(None, {'fields': fields})] 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 Returns a Form class for use in the admin add view. This is used by
add_view and change_view. add_view and change_view.
""" """
if self.declared_fieldsets: if 'fields' in kwargs:
fields = flatten_fieldsets(self.declared_fieldsets) fields = kwargs.pop('fields')
else: else:
fields = None fields = flatten_fieldsets(self.get_fieldsets(request, obj))
if self.exclude is None: if self.exclude is None:
exclude = [] exclude = []
else: else:
@ -1518,10 +1518,10 @@ class InlineModelAdmin(BaseModelAdmin):
def get_formset(self, request, obj=None, **kwargs): def get_formset(self, request, obj=None, **kwargs):
"""Returns a BaseInlineFormSet class for use in admin add/change views.""" """Returns a BaseInlineFormSet class for use in admin add/change views."""
if self.declared_fieldsets: if 'fields' in kwargs:
fields = flatten_fieldsets(self.declared_fieldsets) fields = kwargs.pop('fields')
else: else:
fields = None fields = flatten_fieldsets(self.get_fieldsets(request, obj))
if self.exclude is None: if self.exclude is None:
exclude = [] exclude = []
else: else:
@ -1593,7 +1593,7 @@ class InlineModelAdmin(BaseModelAdmin):
def get_fieldsets(self, request, obj=None): def get_fieldsets(self, request, obj=None):
if self.declared_fieldsets: if self.declared_fieldsets:
return 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)) fields = list(form.base_fields) + list(self.get_readonly_fields(request, obj))
return [(None, {'fields': fields})] return [(None, {'fields': fields})]

View File

@ -64,6 +64,30 @@ class ModelAdminTests(TestCase):
self.assertEqual(ma.get_fieldsets(request, self.band), self.assertEqual(ma.get_fieldsets(request, self.band),
[(None, {'fields': ['name', 'bio', 'sign_date']})]) [(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): def test_field_arguments(self):
# If we specify the fields argument, fieldsets_add and fielsets_change should # If we specify the fields argument, fieldsets_add and fielsets_change should
# just stick the fields into a formsets structure and return it. # just stick the fields into a formsets structure and return it.