From a8d27ca94ea353c3cbc26e515f2bcf62dad89540 Mon Sep 17 00:00:00 2001 From: Diederik van der Boor Date: Tue, 17 Sep 2013 11:02:38 +0200 Subject: [PATCH] Pass ``/admin/app/model/ID/...`` URLs to the correct admin backend. Using the ID field, the correct ``ct_id`` parameter can already be determined. --- docs/changelog.rst | 2 ++ polymorphic/admin.py | 12 ++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index c68d02d..9c6e8c8 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,6 +5,8 @@ Version 0.5.3 (2013-09-17) -------------------------- * Fix TypeError when ``base_form`` was not defined. +* Fix passing ``/admin/app/model/id/XYZ`` urls to the correct admin backend. + There is no need to include a ``?ct_id=..`` field, as the ID already provides enough information. Version 0.5.2 (2013-09-05) diff --git a/polymorphic/admin.py b/polymorphic/admin.py index 454a522..db1c8b2 100644 --- a/polymorphic/admin.py +++ b/polymorphic/admin.py @@ -276,11 +276,19 @@ class PolymorphicParentModelAdmin(admin.ModelAdmin): """ ct_id = int(request.GET.get('ct_id', 0)) if not ct_id: - raise Http404("No ct_id parameter, unable to find admin subclass for path '{0}'.".format(path)) + # See if the path started with an ID. + try: + pos = path.find('/') + object_id = long(path[0:pos]) + except ValueError: + raise Http404("No ct_id parameter, unable to find admin subclass for path '{0}'.".format(path)) + + ct_id = self.model.objects.values_list('polymorphic_ctype_id', flat=True).get(pk=object_id) + real_admin = self._get_real_admin_by_ct(ct_id) resolver = RegexURLResolver('^', real_admin.urls) - resolvermatch = resolver.resolve(path) + resolvermatch = resolver.resolve(path) # May raise Resolver404 if not resolvermatch: raise Http404("No match for path '{0}' in admin subclass.".format(path))