Opened 4 years ago

Closed 4 years ago

#18072 closed Cleanup/optimization (fixed)

ChangeList shouldn't use hardcoded urls

Reported by: kmike Owned by: nobody
Component: contrib.admin Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Some urls are still hardcoded in admin: ChangeList.url_for_result returns implicit relative url

Noticed this while implementing custom AdminSite that shows a changelist of one of the models as an index page (edit links were incorrect):

class RestaurantPanel(admin.AdminSite):
    main_model = None

    def index(self, request, extra_context=None):
        model_admin = self._registry[self.main_model]
        return model_admin.changelist_view(request, extra_context)

This ChangeList subclass fixed the issue for me:

class FixedChangeList(ChangeList):
    def url_for_result(self, result):
        admin_name =
        pk = getattr(result, self.pk_attname)

        return reverse("%s:%s_%s_change" % (
            admin_name, self.opts.app_label, self.opts.module_name
        ), args=[pk])

Change History (4)

comment:1 Changed 4 years ago by ramiro

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 4 years ago by ztatum <z.tatum@…>

Ran into the same problem here. There're also other instances of hardcoded urls in the ModelAdmin.response_add and ModelAdmin.response_change, which I think can go into the same fix.

comment:3 Changed 4 years ago by julien

  • Type changed from Uncategorized to Cleanup/optimization

comment:4 Changed 4 years ago by Ramiro Morales <cramm0@…>

  • Resolution set to fixed
  • Status changed from new to closed

In f51eab796d087439eedcb768cdd312517780940e:

Fixed #18072 -- Made more admin links use reverse() instead of hard-coded relative URLs.

Thanks kmike for the report and initial patch for the changelist->edit
object view link URL.

Other affected links include the delete object one and object history
one (in this case the change had been implemented in commit 5a9e127, this
commit adds admin-quoting of the object PK in a way similar to a222d6e.)

Refs #15294.

Note: See TracTickets for help on using tickets.
Back to Top