Merge pull request #246 from ixc/feature/fix-non-root-parent-admin-lookup

Fix get parent admin when intermediate not root ctype model is registered
fix_request_path_info
Diederik van der Boor 2016-10-14 11:40:10 +02:00 committed by GitHub
commit 3570d10754
1 changed files with 18 additions and 0 deletions

View File

@ -1,12 +1,15 @@
""" """
The child admin displays the change/delete view of the subclass model. The child admin displays the change/delete view of the subclass model.
""" """
import inspect
from django.contrib import admin from django.contrib import admin
from django.core.urlresolvers import resolve from django.core.urlresolvers import resolve
from django.utils import six from django.utils import six
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from .helpers import PolymorphicInlineSupportMixin from .helpers import PolymorphicInlineSupportMixin
from ..admin import PolymorphicParentModelAdmin
class ParentAdminNotRegistered(RuntimeError): class ParentAdminNotRegistered(RuntimeError):
@ -123,6 +126,21 @@ class PolymorphicChildModelAdmin(admin.ModelAdmin):
try: try:
return self.admin_site._registry[parent_model] return self.admin_site._registry[parent_model]
except KeyError: except KeyError:
# Admin is not registered for polymorphic_ctype model, but it may
# be registered for a model in the class ancestry between this
# model and the root parent one.
for klass in inspect.getmro(self.model):
# Ignore model ancestors that are not also subclasses of the
# target ctype model
if not issubclass(klass, parent_model):
continue
# Fetch admin instance for model class (may return None)
model_admin = self.admin_site._registry.get(klass)
# Ignore admin (or None) that isn't a polymorphic parent admin
if not isinstance(model_admin, PolymorphicParentModelAdmin):
continue
return model_admin # Success!
# If we get this far without returning there is no admin available
raise ParentAdminNotRegistered("No parent admin was registered for a '{0}' model.".format(parent_model)) raise ParentAdminNotRegistered("No parent admin was registered for a '{0}' model.".format(parent_model))
def response_post_save_add(self, request, obj): def response_post_save_add(self, request, obj):