diff --git a/docs/changelog.rst b/docs/changelog.rst index b3a4920..ba1c337 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -10,6 +10,7 @@ Changes in git * Fixed ``polymorphic_modelformset_factory()`` usage. * Fixed Python 3 bug for inline formsets. * Fixed CSS for Grappelli, so model choice menu properly overlaps. +* Fixed ``ParentAdminNotRegistered`` exception for models that are registered via a proxy model instead of the real base model. Version 1.0.1 (2016-09-11) diff --git a/polymorphic/admin/childadmin.py b/polymorphic/admin/childadmin.py index 11caa22..0d45f88 100644 --- a/polymorphic/admin/childadmin.py +++ b/polymorphic/admin/childadmin.py @@ -126,20 +126,17 @@ class PolymorphicChildModelAdmin(admin.ModelAdmin): try: return self.admin_site._registry[parent_model] 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. + # Admin is not registered for polymorphic_ctype model, but perhaps it's registered + # for a intermediate proxy model, between the parent_model and this model. 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) + continue # e.g. found a mixin. + + # Fetch admin instance for model class, see if it's a possible candidate. 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 model_admin is not None and isinstance(model_admin, PolymorphicParentModelAdmin): + 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))