Allow specifying a custom base URL when registering ModelAdmin
See original GitHub issueIs 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 optionalbase_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 addbase_url_path
to the list of config attributes, and pass this on when initialisingself.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 usingaccepts_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:
- Created 2 years ago
- Comments:6 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi, because @Nawarrr hasn’t commented in 2 weeks, so I have worked on this issue and come up with a solution.
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?
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?
Go for it @vupham04 thanks for communicating.