question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow specifying a custom base URL when registering ModelAdmin

See original GitHub issue

Is your proposal related to a problem?

Prompted by https://stackoverflow.com/q/71248504/1853523, where a user set up ModelAdmin for a model in an app named ‘users’, which resulted in it registering URLs under /admin/users/ that clashed with Wagtail’s own URLs from wagtail.users. It seems that there’s currently no straightforward way to customise the URL path that ModelAdmin registers - you need to override AdminURLHelper instead.

Describe the solution you’d like

ModelAdmin could support a base_url_path option, e.g.

class BookAdmin(ModelAdmin):
    model = Book
    base_url_path = 'library'  # make URLs appear under `/admin/library/` instead of `/admin/myapp/books/`

This could be implemented as follows:

  • Extend wagtail.contrib.modeladmin.helpers.url.AdminURLHelper.__init__ to accept an optional base_url_path keyword argument
  • In _get_action_url_pattern, use this to build the URL string, falling back on {app_label}/{model_name} if not specified
  • Update wagtail.contrib.modeladmin.options.ModelAdmin to add base_url_path to the list of config attributes, and pass this on when initialising self.url_helper. Note that we need to deal with the possibility that a user might have created a subclass of AdminURLHelper in their own code and overridden __init__, as in this case their code won’t know about the new argument - I’d suggest using accepts_kwarg to check this before calling it, and outputting a deprecation warning if it doesn’t. We can then remove that extra check in a future release.
  • Add tests and documentation

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
vupham04commented, Mar 25, 2022

Hi, because @Nawarrr hasn’t commented in 2 weeks, so I have worked on this issue and come up with a solution.

  1. About the tests, I’m thinking about editing a Model in wagtail\test\modeladmintest\wagtail_hooks.py to include base_url_path, and then in wagtail\contrib\modeladmin\tests\test_modeladmin_edit_handlers.py, in response = self.client.get(…) for the appropriate model, I will edit the URL there to fit base_url_path. Is it okay?

  2. For documentation, in ModelAdmin, should we create a sub-page for customizing base URL, or can I just add it to the Additional tips and tricks?

Should I still create a pull request without tests and documentations, and amend it later?

0reactions
lb-commented, Apr 3, 2022

Go for it @vupham04 thanks for communicating.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Django admin site
ModelAdmin ): pass admin.site.register(Author, AuthorAdmin) ... In the preceding example, the ModelAdmin class doesn't define any custom values (yet).
Read more >
Django custom admin urls not defined - Stack Overflow
I am trying to add two URLs to a model admin. class JobTitleAdmin(admin.ModelAdmin): inlines = [OccupationTagInline, ] model = JobTitle search_fields = ['title ......
Read more >
Django Tutorial Part 4: Django admin site - MDN Web Docs
First we'll show you how to register the models with the admin site, then we'll show you how to login and create some...
Read more >
ModelAdmin — Wagtail Documentation 4.1.1 documentation
The modeladmin module allows you to add any model in your project to the Wagtail admin. You can create customisable listing pages for...
Read more >
Customize the Django Admin With Python
When you registered the PersonAdmin object earlier, it inherited from admin.ModelAdmin . Most of the customization you can do with the Django admin...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found