Feature request: Swappability of main models
See original GitHub issueSo I have this problem with Wagtail that sometimes (or more like, usually) main models (wagtailcore.Page, wagtailimages.Image, etc.) need to have some additional functionality or overrides to existing functionality.
Consider for example making Page translatable without duplicating trees. One would use something like django-modeltranslation to do that but then all the slug/url_path related functions have to be overriden to handle translated urls properly. You could of course write a Mixin and extend all your Page extending models with that, but some of Page’s core functionality doesn’t necessarily use instance.specific
which means that the only way to override Page’s functionality is monkey-patching. And everyone knows how bad monkey-patching is 😃
Django has this very underused feature called swappable models (as used in django.contrib.auth.User). It would be great if Wagtail’s core models would be swappable. This would be as simple as moving all implementation to abstract base models (like what is already the case with Image model) and leaving actual model empty with only swappable option in it’s Meta. Like this:
class Page(BasePage):
class Meta(BasePage.Meta):
swappable = 'WAGTAIL_PAGE_MODEL'
Then implement a simple function get_page_model
which returns the actual Page model and replace all cases of direct Page
class usage with get_page_model()
.
I have attempted a similar refactoring in django-filer and there are really only pros and no cons with this approach. It would all be 100% backwards-compatible. On the other hand, a lot of flexibility is gained this way.
Now I would be willing to provide a pull request for this, but first I wanted to check whether core developers would support such an idea.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:8
- Comments:11 (5 by maintainers)
Top GitHub Comments
@sonnybaker, from
wagtail-modeltranslation
’s perspective a swappablePage
model would be a game changer. It would make the implementation cleaner without recurring to monkey patchingPage
.cc: @DiogoMarques29
Any ongoing activity with this feature? This would be very much needed. Most of the issues I’m having with the Wagtail at the moment comes from the fact that there is no way to extend the Page model.