Wagtail:将BaseSetting添加到ModelAdminGroup

2024-05-14 13:44:13 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我有两种类型的模型:BaseSetting,我使用@register\u settingmodels.Model,然后用ModelAdminModelAdmin\u register显示

目前,我有两个单独的应用程序,它们各自都工作得很好。 然而,我注意到了一个问题。当使用Sign 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的内容。我使用的设置与上一个代码段中导入的设置不同(我不想开始破坏已经可以工作的代码,并且两次使用会导致问题)。但这仍然是一个基础设置,所以概念是一样的

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

我可以立即看到我不能在这里注册设置的问题-它将直接转到“设置”。而且,它也不起作用

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

最好的方法是什么?我试着深入研究文档和堆栈溢出,但没有找到答案

谢谢


Tags: fromimportfalsetruemodelsnulllengthmax

热门问题