Make the admin base_model setting optional.

It can be detected using get_base_polymorphic_model()
This commit is contained in:
Diederik van der Boor
2017-09-30 16:35:02 +02:00
parent 19497960c7
commit cf0cb2478f
5 changed files with 23 additions and 17 deletions
+7 -2
View File
@@ -8,6 +8,7 @@ from django.urls import resolve
from django.utils import six
from django.utils.translation import ugettext_lazy as _
from polymorphic.utils import get_base_polymorphic_model
from ..admin import PolymorphicParentModelAdmin
@@ -25,8 +26,6 @@ class PolymorphicChildModelAdmin(admin.ModelAdmin):
* It adds the base model to the template lookup paths.
* It allows to set ``base_form`` so the derived class will automatically include other fields in the form.
* It allows to set ``base_fieldsets`` so the derived class will automatically display any extra fields.
The ``base_model`` attribute must be set.
"""
base_model = None
base_form = None
@@ -34,6 +33,12 @@ class PolymorphicChildModelAdmin(admin.ModelAdmin):
extra_fieldset_title = _("Contents") # Default title for extra fieldset
show_in_index = False
def __init__(self, model, admin_site, *args, **kwargs):
super(PolymorphicChildModelAdmin, self).__init__(model, admin_site, *args, **kwargs)
if self.base_model is None:
self.base_model = get_base_polymorphic_model(model)
def get_form(self, request, obj=None, **kwargs):
# The django admin validation requires the form to have a 'class Meta: model = ..'
# attribute, or it will complain that the fields are missing.
+6 -3
View File
@@ -18,6 +18,7 @@ from django.utils.http import urlencode
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
from polymorphic.utils import get_base_polymorphic_model
from .forms import PolymorphicModelChoiceForm
@@ -36,9 +37,8 @@ class ChildAdminNotRegistered(RuntimeError):
class PolymorphicParentModelAdmin(admin.ModelAdmin):
"""
A admin interface that can displays different change/delete pages, depending on the polymorphic model.
To use this class, two variables need to be defined:
To use this class, one attribute need to be defined:
* :attr:`base_model` should
* :attr:`child_models` should be a list of (Model, Admin) tuples
Alternatively, the following methods can be implemented:
@@ -50,7 +50,7 @@ class PolymorphicParentModelAdmin(admin.ModelAdmin):
The derived models should *not* register the ModelAdmin, but instead it should be returned by :func:`get_child_models`.
"""
#: The base model that the class uses
#: The base model that the class uses (auto-detected if not set explicitly)
base_model = None
#: The child models that should be displayed
@@ -71,6 +71,9 @@ class PolymorphicParentModelAdmin(admin.ModelAdmin):
super(PolymorphicParentModelAdmin, self).__init__(model, admin_site, *args, **kwargs)
self._is_setup = False
if self.base_model is None:
self.base_model = get_base_polymorphic_model(model)
def _lazy_setup(self):
if self._is_setup:
return