1

我正在尝试创建编辑 Wagtail 的管理界面和分组设置以使其更有意义。为此,我想在一个子菜单中混合 BaseSetting 和 ModelAdmin 模型。

我有两种类型的模型:BaseSetting,我与@register_settingmodels.Model一起使用,然后与ModelAdminmodeladmin_register一起显示

目前我有两个单独的应用程序,它们单独工作得很好。但是,我注意到了一个问题。当使用 ModelAdminGroup 时,显然它只能将 ModelAdmin 类带入其中。

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)

作为参考,这里是导入设置的片段:

@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')
    ])


发生以下错误。

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

我认为部分错误是装饰器也被忽略了。

我尝试的另一种方法是将设置直接添加到我创建模型的应用程序的 models.py 中。

models.py 的内容。我使用的设置与前一个片段中导入的设置不同(我不想开始破坏已经工作的代码并且两次导致问题)。但它仍然是 BaseSetting 所以概念是一样的

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")
    ])

我可以立即看到我无法在此处注册设置的问题 - 它会直接转到“设置”。此外,它也不起作用。

同样,有问题的设置和模型在单独运行时都可以正常工作。我的问题是将它们聚合到一个子菜单中

做这个的最好方式是什么?我曾尝试深入研究文档以及 Stack Overflow,但还没有找到答案。

谢谢!

4

0 回答 0