Opened 7 years ago

Last modified 8 months ago

#28369 new New feature

Provide ModelAdmin hooks for reversing URLs

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

Description

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 by steve yeago, 7 years ago

Has patch: set
Version 0, edited 7 years ago by steve yeago (next)

comment:2 by Tim Graham, 7 years ago

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 by steve yeago, 7 years ago

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.

Example:

# /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 7 years ago by Tim Graham (previous) (diff)

comment:4 by Tim Graham, 7 years ago

Triage Stage: UnreviewedAccepted

comment:5 by steve yeago, 7 years ago

my patch is ready

comment:6 by Tim Graham, 7 years ago

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 7 years ago by Tim Graham (previous) (diff)

comment:7 by Christophe Baldy, 6 years ago

Cc: Christophe Baldy added

comment:8 by Ülgen Sarıkavak, 8 months ago

Cc: Ülgen Sarıkavak added
Note: See TracTickets for help on using tickets.
Back to Top