coredumperror
coredumperror

Reputation: 9110

Implementing a multi-level custom menu in Wagtail

I'm working on enhancing a single-level menu that my site is currently using to great effect. The code is as follows (with irrelevant parts clipped out):

from wagtail.contrib.settings.models import BaseSetting
from modelcluster.models import ClusterableModel

class AbstractCustomMenuItem(models.Model):
    """
    Derive from this model to define one or more CustomMenus.
    """

    url = models.CharField('URL', max_length=200, blank=True,
        help_text='This must be either a fully qualified URL, e.g. https://www.google.com or a local absolute URL, '
                  'e.g. /admin/login'
    )
    page = models.ForeignKey('wagtailcore.Page', null=True, blank=True, on_delete=models.CASCADE, related_name='+',
        help_text='If a Page is selected, the URL field is ignored. The title of the selected Page will be displayed '
                  'if the Link Text field is blank.'
    )
    link_text = models.CharField('Link Text', max_length=50, blank=True)
    is_separator = models.BooleanField( 'Separator', default=False,
        help_text='Separators are used to visually distinguish different sections of a menu.'
    )

    panels = [
        MultiFieldPanel([
            FieldPanel('url', classname='col8 url'), FieldPanel('link_text', classname='col4 link-text'),
        ], classname='url-and-link-text'),
        # This is a bit gnarly, but it was the best way I could find to render the form in a pretty
        # way. I'm using MultiFieldPanel and classname='col8' entirely for formatting, rather than organization.
        MultiFieldPanel([PageChooserPanel('page')], classname='col8 page-chooser'),
        MultiFieldPanel([FieldPanel('is_separator', classname='separator')]),
    ]

    class Meta:
        abstract = True

@register_setting(order=1000)
class Settings(BaseSetting, ClusterableModel):

    ####### FIELD CODE #######
    ...

    ####### FORM CODE #######
    ...

    theme_and_menu_panels = [
        InlinePanel( 'header_menu_items', label='Header Menu Item',
            help_text='You can optionally add a Header Menu to your site, which will appear in the ribbon at the top '
                      'of the page.'
        ),
        InlinePanel('footer_menu_items', label='Footer Menu Item',
            help_text='You can optionally add a Footer Menu to your site, which will appear in the footer.'
        ),
    ]

    ...

    edit_handler = TabbedInterface(
        [
            ...
            ObjectList(theme_and_menu_panels, heading='Theme and Menus', classname='theme-and-menus'),
            ...
        ]
    )


class HeaderMenuItem(Orderable, AbstractCustomMenuItem):
    """
    This class provides the model for the Header Menu Items that can be added to a Site's settings.
    """

    settings = ParentalKey('www.Settings', related_name='header_menu_items', on_delete=models.CASCADE)


class FooterMenuItem(Orderable, AbstractCustomMenuItem):
    """
    This class provides the model for the Footer Menu Items that can be added to a Site's settings.
    """

    settings = ParentalKey('www.Settings', related_name='footer_menu_items', on_delete=models.CASCADE)

This gives my code the ability to assign separate, custom Header and Footer menus.

Now, however, I need to upgrade this code to allow the Header menu items to optionally have their own submenu beneath them. And I figured that I could do basically the same thing I did to create the MenuItems in the first place, and just parent them beneath the HeaderMenuItem class, rather than beneath Settings.

So I changed the HeaderMenuItem class, and added HeaderMenuDropdownItem:

class HeaderMenuItem(Orderable, ClusterableModel, AbstractCustomMenuItem):
    """
    This class provides the model for the Header Menu Items that can be added to a Site's settings.
    """

    settings = ParentalKey('www.Settings', related_name='header_menu_items', on_delete=models.CASCADE)

    panels = [
        MultiFieldPanel([
            FieldPanel('url', classname='col8 url'), FieldPanel('link_text', classname='col4 link-text'),
        ], classname='url-and-link-text'),
        # This is a bit gnarly, but it was the best way I could find to render the form in a pretty
        # way. I'm using MultiFieldPanel and classname='col8' entirely for formatting, rather than organization.
        MultiFieldPanel([PageChooserPanel('page')], classname='col8 page-chooser'),
        MultiFieldPanel([FieldPanel('is_separator', classname='separator')]),
        InlinePanel('dropdown_items', classname='dropdown-items'),
    ]


class HeaderMenuDropdownItem(Orderable, AbstractCustomMenuItem):
    """
    This class provides the model for the Header Menu Dropdown items.
    """

    header_menu_item = ParentalKey('www.HeaderMenuItem', related_name='dropdown_items', on_delete=models.CASCADE)

Unfortunately, I now get the following exception when I load the Wagtail admin page for editing the Settings class:

File "/.../django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/.../django/core/handlers/base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "/.../django/core/handlers/base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/.../django/views/decorators/cache.py" in _cache_controlled
  31.             response = viewfunc(request, *args, **kw)

File "/.../wagtail/admin/urls/__init__.py" in wrapper
  102.             return view_func(request, *args, **kwargs)

File "/.../wagtail/admin/decorators.py" in decorated_view
  34.             return view_func(request, *args, **kwargs)

File "/.../wagtail/contrib/settings/views.py" in edit
  83.             instance=instance, form=form, request=request)

File "/.../wagtail/admin/edit_handlers.py" in bind_to_instance
  152.         new.on_instance_bound()

File "/.../wagtail/admin/edit_handlers.py" in on_instance_bound
  294.                                                    request=self.request))

File "/.../wagtail/admin/edit_handlers.py" in bind_to_instance
  152.         new.on_instance_bound()

File "/.../wagtail/admin/edit_handlers.py" in on_instance_bound
  294.                                                    request=self.request))

File "/.../wagtail/admin/edit_handlers.py" in bind_to_instance
  152.         new.on_instance_bound()

File "/.../wagtail/admin/edit_handlers.py" in on_instance_bound
  708.                                                     request=self.request))

File "/.../wagtail/admin/edit_handlers.py" in bind_to_instance
  152.         new.on_instance_bound()

File "/.../wagtail/admin/edit_handlers.py" in on_instance_bound
  294.                                                    request=self.request))

File "/.../wagtail/admin/edit_handlers.py" in bind_to_instance
  152.         new.on_instance_bound()

File "/.../wagtail/admin/edit_handlers.py" in on_instance_bound
  693.         self.formset = self.form.formsets[self.relation_name]

Exception Type: AttributeError at /admin/settings/www/settings/3/
Exception Value: 'HeaderMenuItemForm' object has no attribute 'formsets'

What am I doing wrong? Can a ParentalKey simply not be nested inside another ParentalKey? If not, how could I implement this multi-level menu? Maybe I'm going about this all wrong from the start?

Upvotes: 1

Views: 1678

Answers (1)

Travis
Travis

Reputation: 572

Have you looked into wagtailmenus? It might save you some development time and effort.

Upvotes: 1

Related Questions