Sam Bobel
Sam Bobel

Reputation: 1824

How do I reverse the URL for an admin action?

I'm posting this because I searched stackoverflow and docs for a long time without finding an answer -- hopefully this helps somebody out.

The question is, for testing purposes, how do I find the URL that's related to admin actions for a specific model?

Admin model urls can all be found by reverse(admin:appname_modelname_*), where * is the action (change, delete, etc). But I couldn't find one for the admin actions, and since I was defining custom actions, I'd like to get the url.

Upvotes: 3

Views: 2075

Answers (3)

b00bl1k
b00bl1k

Reputation: 21

The URL for all custom actions is reverse(admin:<appname>_<modelname>_changelist), but the action name is specified in the action field of the POST data.

Upvotes: 2

Grant Anderson
Grant Anderson

Reputation: 362

This took a fair bit of digging, I couldn't find anything in the Django docs about it and I ended up having to inspect the source code of a third party library.

Essentially there are 2 URL patterns, one for bulk actions and one for object actions:

  • Bulk: r'admin/<app_label>/<model_name>/actions/(?P<tool>\\w+)/$'
  • Object: r'admin/<app_label>/<model_name>/(?P<pk>.+)/actions/(?P<tool>\\w+)/$'

The URL name pattern is <app_label>_<model_name>_actions

Therefore we can reverse the bulk view:

  • Using args: reverse("admin:<app_label>_<model_name>_actions", args=["foo"])
  • Using kwargs: reverse("admin:<app_label>_<model_name>_actions", kwargs={"tool": "foo"})

and reverse the object view:

  • Using args: reverse("admin:<app_label>_<model_name>_actions", args=[1, "foo"])
  • Using kwargs: reverse("admin:<app_label>_<model_name>_actions", kwargs={"pk": 1, "tool": "foo"})

Upvotes: 3

Sam Bobel
Sam Bobel

Reputation: 1824

The answer, which is hard to find, is that actions are referenced by reverse(admin:appname_modelname_changelist)

Upvotes: 0

Related Questions