ModelAdmin form_fields_exclude is ignored on models with a panels definition
See original GitHub issueIssue Summary
According to the docs and PR #3295, you should be able to add form_fields_exclude
or get_form_fields_exclude
on a ModelAdmin
to exclude certain fields from rendering on the admin. When I tried it, it didn’t work. Upon further investigation, I found out that it has never actually worked. Issue #2759 is still open.
The issue is rooted in extract_panel_definitions_from_model_class
function. When you have a model that has panels
, it returns the panels as is, without filtering the excluded fields.
So a couple things that stand out to me:
- Maybe mention in the docs that this doesn’t actually work
- Find a way to make it work for all panel types
Steps to Reproduce
- Create a
ModelAdmin
inwagtail_hooks.py
- Set
form_fields_exclude = ['some_field']
as a class attribute - Create a new instance of that model in the Wagtail admin
- Look in confusion at the still present
some_field
Workaround
I’m currently working around the issue by creating a custom CreateView
for my ModelAdmin
s and overriding get_edit_handlers
to remove panels I don’t want.
class PatchedCreateView(CreateView):
def get_edit_handler_class(self):
if hasattr(self.model, 'edit_handler'):
edit_handler = self.model.edit_handler
else:
fields_to_exclude = self.model_admin.get_form_fields_exclude(
request=self.request
)
panels = extract_panel_definitions_from_model_class(
self.model,
exclude=fields_to_exclude
)
# Filter out fields in fields_to_exclude
panels = [
panel for panel in panels
if getattr(panel, 'field_name', None) not in fields_to_exclude
]
edit_handler = ObjectList(panels)
return edit_handler.bind_to_model(self.model)
Only problem is this doesn’t work for panels without field names. But in my particular case that’s okay.
Technical details
- Python version: 3.6.4
- Django version: 1.11.10
- Wagtail version: 1.13.1
Issue Analytics
- State:
- Created 6 years ago
- Comments:11 (9 by maintainers)
Top GitHub Comments
Now reverted in e84b4a0 (master) / 6ccd665 (stable/2.1.x).
Hi,
Thanks for the suggestions. I agree with the point from Matt and ababic that form_fields_exclude was not meant for this and am for reverting it. Optionally it could generate a warning maybe, saying that the form_fields_exclude option was ignored because of the panel definition.