1

I am trying to add a dynamic instead of a hard coded get_inspect_url method to a PageModel:

class MyModel(Page):
    # ...
    # this is what I have now, which breaks in prod due to a different admin url
    def get_inspect_url(self):
        inspect_url = f"/admin/myapp/mymodel/inspect/{self.id}/"   
        return inspect_url

I know that you can use the ModelAdmin.url_helper_class in wagtail_hooks.py to get admin urls for an object, but I can't figure a way to do the same on the model level as a class method. Is there a way?

Jean Zombie
  • 607
  • 1
  • 9
  • 28

2 Answers2

1

thanks to @Andrey Maslov tip I found a way:

from django.urls import reverse

    def get_inspect_url(self):
        return reverse(
            f"{self._meta.app_label}_{self._meta.model_name}_modeladmin_inspect",
            args=[self.id],
        )

The basic form of a Wagtail admin url is:

[app_label]_[model_name]_modeladmin_[action]

Just change the [action] to one of the following to generated other instance level admin urls:

  • edit
  • delete
Jean Zombie
  • 607
  • 1
  • 9
  • 28
0

if you have in you urls.py line like this

path(MyModel/view/<id:int>/', YourViewName, name='mymodel-info'),

then you can add to your models.py this lines

from django.urls import reverse
...

class MyModel(Page):

    def get_inspect_url(self):
        inspect_url = reverse('mymodel-info', kwargs={'id': self.id})
        return inspect_url
Andrey Maslov
  • 1,396
  • 1
  • 8
  • 10
  • 1
    I don't have an extra `view` associated with my model, since Wagtail takes care of that. So no distinct url path in `urls.py`. But I just figured another way: Importing my admin base url from my settings with `settings.WAGTAIL_ADMIN_URL` and put that in. – Jean Zombie Jul 01 '20 at 10:28
  • 1
    then may be this answer will be usefull for you [https://stackoverflow.com/a/2930241/7186864](https://stackoverflow.com/a/2930241/7186864)/ do the same with `reverse` – Andrey Maslov Jul 01 '20 at 10:30