Opened 5 years ago

Last modified 3 years ago

#28369 assigned New feature

Provide ModelAdmin hooks for reversing URLs

Reported by: steve yeago Owned by: Abhishek Bera
Component: contrib.admin Version: dev
Severity: Normal Keywords:
Cc: Christophe Baldy Triage Stage: Accepted
Has patch: yes Needs documentation: yes
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no


Django admin could provide a hook for cases where there are additional parameters necessary to build an add or change url. Current hook "response_change" makes many hard-coded presumptions about how to reverse and add or change url. While its a nice hook to have for some cases, its so verbose that overriding it for this purpose is very hard.

Change History (8)

comment:1 Changed 5 years ago by steve yeago

Has patch: set
Last edited 5 years ago by Tim Graham (previous) (diff)

comment:2 Changed 5 years ago by Tim Graham

Easy pickings: unset
Needs documentation: set
Needs tests: set
Summary: Django Admin "Save and add another" NoReverseMatch with custom urlsProvide ModelAdmin hooks for reversing URLs

I lamented on django-developers about the never ending number of ModelAdmin hooks. I fear we'll end up with an unmaintainable mess if we make every little thing customizable. Those methods would also need to be documented and tested. Perhaps you could describe your use case in more detail to help justify the additional complexity.

comment:3 Changed 5 years ago by steve yeago

Well in some ways I think the patch creates some consistency given that there is already a hook for the "view on site" url in the admin. Grepping around, there could be some savings of lines get_*_url in the tests where basically the same hooks already exists.


# /parents/1/child_changelist

class ParentAdminSite(admin.Site):
    def get_urls(self):
        return super(ParentAdminSite, self).get_urls() + [
            url(r'^(?P<parent_id>%s)/children/', include(ChildObjectAdmin(imodels.ChildObject, self).urls)))

class ChildObjectAdmin(admin.ModelAdmin):
    def changelist_view(self, request, parent_id=None):
        if parent_id:
            self.parent_id = parent_id
            self.parent = get_object_or_404(models.Parent, parent_id=parent_id)
        return super(ChildObjectAdmin, self).changelist_view(request)

    def get_queryset(self, request):
        return super(ChildObjectAdmin, self).get_queryset(request).filter(parent=self.parent)

The current implementation presumes that a modeladmin is registered under a namespace with no other args or kwargs, but that may not be the case if someone has included additional url args in get_urls(). Almost everything about the admin works fine out of the box in this situation aside from the somewhat superficial problem of where the user is redirect post-save because, the urls that are being reversed deep in response_change (and subsequently response_post_save_change) are hard-coded.

Last edited 5 years ago by Tim Graham (previous) (diff)

comment:4 Changed 5 years ago by Tim Graham

Triage Stage: UnreviewedAccepted

comment:5 Changed 5 years ago by steve yeago

my patch is ready

comment:6 Changed 5 years ago by Tim Graham

The patch still lacks documentation of the new ModelAdmin methods (see the PatchReviewChecklist). You should update the ticket's flags as indicated in the "According to the ticket's flags, the next step(s) to move this issue forward are:" box below the ticket description.

Last edited 5 years ago by Tim Graham (previous) (diff)

comment:7 Changed 4 years ago by Christophe Baldy

Cc: Christophe Baldy added

comment:8 Changed 3 years ago by Abhishek Bera

Owner: changed from nobody to Abhishek Bera
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top