Factored shared Inline/ModelAdmin.get_fields() logic into BaseModelAdmin.

BaseModelAdmin.get_fields() was previously unused/untested.
This commit is contained in:
Tim Graham 2017-06-07 17:11:02 -04:00 committed by GitHub
parent 681d2599ee
commit c16aa69101
1 changed files with 9 additions and 11 deletions

View File

@ -290,7 +290,11 @@ class BaseModelAdmin(metaclass=forms.MediaDefiningClass):
""" """
Hook for specifying fields. Hook for specifying fields.
""" """
if self.fields:
return self.fields return self.fields
# _get_form_for_get_fields() is implemented in subclasses.
form = self._get_form_for_get_fields(request, obj)
return list(form.base_fields) + list(self.get_readonly_fields(request, obj))
def get_fieldsets(self, request, obj=None): def get_fieldsets(self, request, obj=None):
""" """
@ -598,11 +602,8 @@ class ModelAdmin(BaseModelAdmin):
'delete': self.has_delete_permission(request), 'delete': self.has_delete_permission(request),
} }
def get_fields(self, request, obj=None): def _get_form_for_get_fields(self, request, obj):
if self.fields: return self.get_form(request, obj, fields=None)
return self.fields
form = self.get_form(request, obj, fields=None)
return list(form.base_fields) + list(self.get_readonly_fields(request, obj))
def get_form(self, request, obj=None, **kwargs): def get_form(self, request, obj=None, **kwargs):
""" """
@ -1942,11 +1943,8 @@ class InlineModelAdmin(BaseModelAdmin):
return inlineformset_factory(self.parent_model, self.model, **defaults) return inlineformset_factory(self.parent_model, self.model, **defaults)
def get_fields(self, request, obj=None): def _get_form_for_get_fields(self, request, obj=None):
if self.fields: return self.get_formset(request, obj, fields=None).form
return self.fields
form = self.get_formset(request, obj, fields=None).form
return list(form.base_fields) + list(self.get_readonly_fields(request, obj))
def get_queryset(self, request): def get_queryset(self, request):
queryset = super().get_queryset(request) queryset = super().get_queryset(request)