From c16aa69101a3722061be4a2ed435865557b99157 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Wed, 7 Jun 2017 17:11:02 -0400 Subject: [PATCH] Factored shared Inline/ModelAdmin.get_fields() logic into BaseModelAdmin. BaseModelAdmin.get_fields() was previously unused/untested. --- django/contrib/admin/options.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index d0941a44a4..5a99a2970d 100644 --- a/django/contrib/admin/options.py +++ b/django/contrib/admin/options.py @@ -290,7 +290,11 @@ class BaseModelAdmin(metaclass=forms.MediaDefiningClass): """ Hook for specifying fields. """ - return self.fields + if 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): """ @@ -598,11 +602,8 @@ class ModelAdmin(BaseModelAdmin): 'delete': self.has_delete_permission(request), } - def get_fields(self, request, obj=None): - if self.fields: - 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_for_get_fields(self, request, obj): + return self.get_form(request, obj, fields=None) def get_form(self, request, obj=None, **kwargs): """ @@ -1942,11 +1943,8 @@ class InlineModelAdmin(BaseModelAdmin): return inlineformset_factory(self.parent_model, self.model, **defaults) - def get_fields(self, request, obj=None): - if self.fields: - 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_form_for_get_fields(self, request, obj=None): + return self.get_formset(request, obj, fields=None).form def get_queryset(self, request): queryset = super().get_queryset(request)