wagtail: ImageChooserField does not work in nested forms.

After this feature we are able to create nested related objects in the same form. But when we use ImageChooserField at the second level or deeper - Wagtail shows regular select instead of ImageChooserField. On the screenshot, ImageChooserField is setted for the logo field.

screenshot from 2019-03-07 21-07-27

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 2
  • Comments: 15 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Got SnippetChooserPanel working with this:

from wagtail.admin.edit_handlers import InlinePanel as BaseInlinePanel

class InlinePanel(BaseInlinePanel):
    def widget_overrides(self):
        widgets = {}
        child_edit_handler = self.get_child_edit_handler()
        for handler_class in child_edit_handler.children:
            widgets.update(handler_class.widget_overrides())
        widget_overrides = {self.relation_name: widgets}
        return widget_overrides

Basically the nested-second-layer InlinePanel wasn’t providing the Page model with any widgets, while the first layer InlinePanel was doing so. Haven’t tested it with different combinations (third layer, all layers having custom panels, etc).

nested inlinepanel

The suggestion above for creating a custom InlinePanel worked in my case, as such:

from wagtail.admin.edit_handlers import InlinePanel


class NestedInlinePanel(InlinePanel):
    def widget_overrides(self):
        widgets = {}

        child_edit_handler = self.get_child_edit_handler()

        for handler_class in child_edit_handler.children:
            widgets.update(handler_class.widget_overrides())

        widget_overrides = {self.relation_name: widgets}

        return widget_overrides

One thing of note is that the title and help_text don’t work with the custom NestedInlinePanel.

NestedInlinePanel(
    "authors",
    # Heading and help text are not rendered in the form UI
    heading="Authors",
    help_text="Select one or more authors who contributed to this article",
 ),

The same issue occurs with PageChooserPanel: image

+2 NestedInlinePanel ignores max_num

+1 that the NestedInlinePanel provided above fixes the issue for 2.14.1. Like @brylie above, it appears none of the settings carry forward; I am trying to limit the number of forms by setting max_num, but it doesn’t appear to do anything.

Copying and overriding the default admin/templates/wagtailadmin/edit_handlers/inline_panel.js, I threw a few console.logs in there to see what was going on. The self.formset.max_num for the NestedInlinePanel is 1000. I’m not familiar enough with Wagtail internals to know what to do with that info, but maybe it will help someone else diagnose what’s going on.

Got SnippetChooserPanel working with this:

from wagtail.admin.edit_handlers import InlinePanel as BaseInlinePanel

class InlinePanel(BaseInlinePanel):
    def widget_overrides(self):
        widgets = {}
        child_edit_handler = self.get_child_edit_handler()
        for handler_class in child_edit_handler.children:
            widgets.update(handler_class.widget_overrides())
        widget_overrides = {self.relation_name: widgets}
        return widget_overrides

Basically the nested-second-layer InlinePanel wasn’t providing the Page model with any widgets, while the first layer InlinePanel was doing so. Haven’t tested it with different combinations (third layer, all layers having custom panels, etc).

@MaziyarMK thank you for the code snippet, I used on a project running Wagtail 2.9.3 and works as expected 👌 .