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.

ModelAdmin form_fields_exclude is ignored on models with a panels definition

See original GitHub issue

Issue 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:

  1. Maybe mention in the docs that this doesn’t actually work
  2. Find a way to make it work for all panel types

Steps to Reproduce

  1. Create a ModelAdmin in wagtail_hooks.py
  2. Set form_fields_exclude = ['some_field'] as a class attribute
  3. Create a new instance of that model in the Wagtail admin
  4. Look in confusion at the still present some_field

Workaround

I’m currently working around the issue by creating a custom CreateView for my ModelAdmins 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:open
  • Created 6 years ago
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
gasmancommented, May 16, 2018

Now reverted in e84b4a0 (master) / 6ccd665 (stable/2.1.x).

0reactions
lucasmoeskopscommented, May 15, 2018

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot use custom form in contrib.modeladmin with some ...
Supposing we have the following model class Author(models.Model): first_name = models.CharField(max_length=50) last_name = models.
Read more >
different fields for add and change pages in admin
First have a look at source of ModelAdmin class' get_form and get_formsets methods located in django ...
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 Reddit Thread

No results found

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