From 46e41a6c1ca9f1fe3f8e416a0ed799cc1b651875 Mon Sep 17 00:00:00 2001 From: Diederik van der Boor Date: Wed, 17 Feb 2016 14:49:40 +0100 Subject: [PATCH] Fix changeform_view() redirection to the child admin site. This method is normally called from `add_view()` and `change_view()`. However, some third party modules (such as django-reversion) call this method directly. By redirecting those calls to the child admin, their views also display the proper admin views. --- docs/changelog.rst | 10 +++++++++- polymorphic/admin.py | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 1d807ad..1a61b16 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,6 +1,14 @@ Changelog ========= +Changes in git +-------------- + +* Fixed support for ``PolymorphicManager.from_queryset()`` for custom query sets. +* 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()``. + + Version 0.9 (2016-02-17) ------------------------ @@ -9,7 +17,7 @@ Version 0.9 (2016-02-17) * Fix Django 1.9 handling of custom URLs. The new change-URL redirect overlapped any custom URLs defined in the child admin. * Fix Django 1.9 support in the admin. -* Fix missing ``history_view`` redirection to the child admin, which is important for django-reversion_ support. +* Fix missing ``history_view()`` redirection to the child admin, which is important for django-reversion_ support. See the documentation for hints for :ref:`django-reversion-compare support `. diff --git a/polymorphic/admin.py b/polymorphic/admin.py index 5bbd730..07fe2fc 100644 --- a/polymorphic/admin.py +++ b/polymorphic/admin.py @@ -262,6 +262,18 @@ class PolymorphicParentModelAdmin(admin.ModelAdmin): real_admin = self._get_real_admin(object_id) return real_admin.change_view(request, object_id, *args, **kwargs) + if django.VERSION >= (1, 7): + def changeform_view(self, request, object_id=None, *args, **kwargs): + # The `changeform_view` is available as of Django 1.7, combining the add_view and change_view. + # As it's directly called by django-reversion, this method is also overwritten to make sure it + # also redirects to the child admin. + if object_id: + real_admin = self._get_real_admin(object_id) + return real_admin.changeform_view(request, object_id, *args, **kwargs) + else: + # Add view. As it should already be handled via `add_view`, this means something custom is done here! + return super(PolymorphicParentModelAdmin, self).changeform_view(request, object_id, *args, **kwargs) + def history_view(self, request, object_id, extra_context=None): """Redirect the history view to the real admin.""" real_admin = self._get_real_admin(object_id)