Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #18681 -- BaseModelAdmin.get_form and InlineModelAdmin.get_form…

…set no longer bypass get_fieldsets

Thanks msopacua for the report.
  • Loading branch information...
commit 23e1b59cf2ad6a75637dd0273973e657e48e317e 1 parent f10e9af
@loic loic authored timgraham committed
Showing with 32 additions and 8 deletions.
  1. +8 −8 django/contrib/admin/options.py
  2. +24 −0 tests/modeladmin/tests.py
View
16 django/contrib/admin/options.py
@@ -498,7 +498,7 @@ def get_fieldsets(self, request, obj=None):
"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 @@ def get_form(self, request, obj=None, **kwargs):
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 @@ def get_extra(self, request, obj=None, **kwargs):
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 @@ def is_valid(self):
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})]
View
24 tests/modeladmin/tests.py
@@ -64,6 +64,30 @@ def test_default_fieldsets(self):
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.
Please sign in to comment.
Something went wrong with that request. Please try again.