1

I am trying to create edit the admin interface of Wagtail and grouping setting to make more sense. To do this I want to mix BaseSetting and ModelAdmin models within one submenu.

I have two types of models for it: BaseSetting, that I use with @register_setting and models.Model, which then are displayed with ModelAdmin and modeladmin_register

Currently I have separate apps for both and individually they are working just fine. However, I have noticed a problem. When usign ModelAdminGroup, apparently it can only take ModelAdmin classes into it.

Contents of admin.py

from wagtail.contrib.modeladmin.options import (
    ModelAdmin,
    ModelAdminGroup,
    modeladmin_register,
)

# Import app self model
from .models import Brand

# Import sidebar settings from site_settings
from site_settings.models import SidebarSettings

class BrandAdmin(ModelAdmin):
    """ Brand Admin model """

    model = Brand
    menu_label = "Brands"
    menu_icon = "placeholder"

    add_to_settings_menu = False
    exclude_from_explorer = False
    list_display = ("brand_name", "affiliate_program", "contact",)
    search_fields = ("brand_name", "affiliate_program",
                     "affiliate_link", "contact",)


class Commercial(ModelAdminGroup):
    """ Group the """
    menu_label = "Commercial"
    menu_order = 900
    menu_icon = 'placeholder'
    items = (BrandAdmin, SidebarSettings)


modeladmin_register(Commercial)

For reference, here is the snippet of the setting imported:

@register_setting
class SidebarSettings(BaseSetting):
    skyscraper_ad = models.ForeignKey(
        'wagtailimages.Image',
        blank=True,
        null=True,
        on_delete=models.SET_NULL,
        related_name='+'
    )

    skyscraper_panels = [
        ImageChooserPanel('skyscraper_ad')
    ]

    edit_handler = TabbedInterface([
        ObjectList(skyscraper_panels, heading='Skyscraper')
    ])


The following error occurs.

TypeError: SidebarSettings() got an unexpected keyword argument 'parent'

I assume part of the error is that the decorator gets ignored as well.

Another way I tried is to add the the setting directly to the models.py of my app, where I create the Models.

Contents of models.py. I am using a different setting than one imported in the previous snippet (I don't want to start breaking code that works already and having it twice causes issues). But it's still a BaseSetting so concept is same

from django.db import models
from wagtail.admin.edit_handlers import (
    TabbedInterface,
    ObjectList,
    FieldPanel,
    MultiFieldPanel,
    StreamFieldPanel
)

from wagtail.contrib.settings.models import BaseSetting

from wagtail.images.edit_handlers import ImageChooserPanel

# Create your models here.
class Brand(models.Model):
    """ A model for registering brands """

    brand_name = models.CharField(max_length=50, blank=False, null=False)
    affiliate_program = models.CharField(max_length=50, blank=False, null=False)
    affiliate_link = models.CharField(max_length=50, blank=True, null=True)
    contact = models.CharField(max_length=50, blank=True, null=True)

    logos = models.CharField(max_length=50, blank=True, null=True)
    temp = models.CharField(max_length=50, blank=True, null=True)
    temp_2 = models.CharField(max_length=50, blank=True, null=True)

    basic_info_panels = [
        MultiFieldPanel([
            FieldPanel('brand_name'),
            FieldPanel('affiliate_program'),
            FieldPanel('affiliate_link'),
            FieldPanel('contact'),

    ], heading="collapsible panel test", classname="collapsible")]

    logos_panels = [
        FieldPanel('logos')
    ]

    extra_info_panels = [
        FieldPanel('temp'),
        FieldPanel('temp_2')
    ]

    edit_handler = TabbedInterface([
        ObjectList(basic_info_panels, heading="Basic Info"),
        ObjectList(logos_panels, heading="Logos"),
        ObjectList(extra_info_panels, heading="Extra info")
    ])

    def __str__(self):
        return self.brand_name


class Sidebar(BaseSetting):
    """  """

    test = models.CharField(max_length=50, blank=True, null=True)
    test_panel = [
        FieldPanel('test')
    ]

    edit_handler = TabbedInterface([
        ObjectList(test_panel, heading="Test")
    ])

I can immediately see the problem that I cannot register a setting here - it would go directly to "Settings". Also, it does not work either.

Again both the settings and models in question work with no issues when running separately. My problem is with aggregating them into one submenu

What is the best way to do this? I have tried digging into documentation as well as Stack Overflow, but have not found an answer to this.

Thanks!

0 Answers0