Documentation request: more `modeladmin` examples
See original GitHub issueIt’s great that modeladmin
got included in 1.5, but it would be great to see some more documentation, particularly in the form of examples. We’re trying to do something relatively simple - make one of our model fields hidden and auto-populated from request.user
- but can’t figure out the “canonical” method (or even one that works satisfactorily).
So far we’ve tried:
- Override
create_view_class
andedit_view_class
on ourModelAdmin
subclass to views that reference a custom form where the field widget is overridden. This works, but the label is still displayed in the view. - Override
create_view_class
andedit_view_class
on ourModelAdmin
subclass to views that reference a custom form whereexcludes
lists our field. This raises an error somewhere in the guts of Wagtail that expects that field to be present. - Similar to 1, override the views, override
get_initial
to return information fromrequest.user
into the form, and definepanels
on the models with aFieldPanel('user', widget=HiddenInput())
which has a similar issue to 1 - it works, but the field label is still visible on the form. - Progressing from 3, remove the field from
panels
completely - this successfully removes the field from view (and from the form) but then we can’t accessrequest
in order to auto-populate the value.
I’m not necessarily asking for the right answer for how to achieve what we need, more I wanted to point out that though having modeladmin
is great, the indication in the docs that “the ModelAdmin class has a large number of methods that you can override or extend, allowing you to customise the behaviour”, while true, has led us down several blind alleys in trying to achieve something pretty basic.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:6 (3 by maintainers)
Top GitHub Comments
Hey @jimr. I would consider the label still showing for a hidden field as a bug, yes, and probably worth raising as it’s own issue, if you wouldn’t mind? I believe it’s a ‘panels’ thing, though, rather than a modeladmin-specific one.
I definitely agree with you about getting some guidance into the docs regarding overriding of model forms. If issues like #2717 and this are already cropping up, then it’s clear that more people will run into similar difficulties in future. I guess it’s a double-edged sword when you use common Django components. There are a lot of methods there that developers will be used to extending/overriding, which I would hope makes their lives easier in the main. But, Wagtail has it’s ‘edit handler’ layer that complicates things when it comes to forms. I can’t claim to know the full ins and outs of that myself, but @gasman may be able to offer some guidance about how we might possibly work around that in future.
An important thing to bare in mind here, is that
modeladmin
is still only handling the adding and editing for non-page models at this stage. That’s mostly why the customisation available on the ModelAdmin class itself is focussed around ‘listing’ behaviour (that is the only view that is used consistently). In the future, I’d hope for the page-management views and other things in core to become class-based; which would make it possible for modeladmin to extend them, and offer a more consistent level of control for everything.Now addressed by #2753 / #2902.