diff --git a/docs/changelog.rst b/docs/changelog.rst index ca89ba4..ef366d4 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -8,6 +8,10 @@ Changes in git * Fixed Django 1.7 ``changeform_view()`` redirection to the child admin site. This fixes custom admin code that uses these views, such as django-reversion_'s ``revision_view()`` / ``recover_view()``. * Fixed ``.only('pk')`` field support. +* Fixed ``object_history_template`` breadcrumb. + **NOTE:** when using django-reversion_ / django-reversion-compare_, make sure to implement + a ``admin/polymorphic/object_history.html`` template in your project that extends + from ``reversion/object_history.html`` or ``reversion-compare/object_history.html`` respectively. Version 0.9 (2016-02-17) diff --git a/docs/third-party.rst b/docs/third-party.rst index 883c66a..1bac44b 100644 --- a/docs/third-party.rst +++ b/docs/third-party.rst @@ -12,6 +12,7 @@ However, they require more setup than standard models. That's become: * Polymorphic models use `multi-table inheritance `_. See the `reversion documentation `_ how to deal with this by adding a ``follow`` field for the primary key. +* Both admin classes redefine ``object_history_template``. Example @@ -28,14 +29,14 @@ The admin :ref:`admin-example` becomes: from .models import ModelA, ModelB, ModelC - class ModelAChildAdmin(PolymorphicChildModelAdmin): + class ModelAChildAdmin(PolymorphicChildModelAdmin, VersionAdmin): base_model = ModelA base_form = ... base_fieldsets = ( ... ) - class ModelBAdmin(VersionAdmin, ModelAChildAdmin): + class ModelBAdmin(ModelAChildAdmin, VersionAdmin): # define custom features here class ModelCAdmin(ModelBAdmin): @@ -53,6 +54,19 @@ The admin :ref:`admin-example` becomes: revisions.register(ModelC, follow=['modelb_ptr']) admin.site.register(ModelA, ModelAParentAdmin) +Redefine a :file:`admin/polymorphic/object_history.html` template, so it combines both worlds: + +.. code-block:: html+django + + {% extends 'reversion/object_history.html' %} + {% load polymorphic_admin_tags %} + + {% block breadcrumbs %} + {% breadcrumb_scope base_opts %}{{ block.super }}{% endbreadcrumb_scope %} + {% endblock %} + +This makes sure both the reversion template is used, and the breadcrumb is corrected for the polymorphic model. + .. _django-reversion-compare-support: django-reversion-compare support diff --git a/polymorphic/admin.py b/polymorphic/admin.py index 07fe2fc..ad40a90 100644 --- a/polymorphic/admin.py +++ b/polymorphic/admin.py @@ -525,6 +525,25 @@ class PolymorphicChildModelAdmin(admin.ModelAdmin): "admin/delete_confirmation.html" ] + @property + def object_history_template(self): + opts = self.model._meta + app_label = opts.app_label + + # Pass the base options + base_opts = self.base_model._meta + base_app_label = base_opts.app_label + + return [ + "admin/%s/%s/object_history.html" % (app_label, opts.object_name.lower()), + "admin/%s/object_history.html" % app_label, + # Added: + "admin/%s/%s/object_history.html" % (base_app_label, base_opts.object_name.lower()), + "admin/%s/object_history.html" % base_app_label, + "admin/polymorphic/object_history.html", + "admin/object_history.html" + ] + def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None): context.update({ 'base_opts': self.base_model._meta, @@ -537,6 +556,16 @@ class PolymorphicChildModelAdmin(admin.ModelAdmin): } return super(PolymorphicChildModelAdmin, self).delete_view(request, object_id, extra_context) + def history_view(self, request, object_id, extra_context=None): + # Make sure the history view can also display polymorphic breadcrumbs + context = { + 'base_opts': self.base_model._meta, + } + if extra_context: + context.update(extra_context) + return super(PolymorphicChildModelAdmin, self).history_view(request, object_id, extra_context=context) + + # ---- Extra: improving the form/fieldset default display ---- def get_fieldsets(self, request, obj=None): diff --git a/polymorphic/templates/admin/polymorphic/object_history.html b/polymorphic/templates/admin/polymorphic/object_history.html new file mode 100644 index 0000000..4306b53 --- /dev/null +++ b/polymorphic/templates/admin/polymorphic/object_history.html @@ -0,0 +1,6 @@ +{% extends "admin/object_history.html" %} +{% load polymorphic_admin_tags %} + +{% block breadcrumbs %} + {% breadcrumb_scope base_opts %}{{ block.super }}{% endbreadcrumb_scope %} +{% endblock %}