问题描述
我正在尝试创建编辑 Wagtail 的管理界面和分组设置以使其更有意义。为此,我想在一个子菜单中混合使用 BaseSetting 和 ModelAdmin 模型。
我有两种类型的模型:BaseSetting,与 @register_setting 和 models.Model 一起使用,然后显示使用 ModelAdmin 和 modeladmin_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_link",)
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,null=False)
affiliate_link = models.CharField(max_length=50,null=True)
contact = models.CharField(max_length=50,null=True)
logos = models.CharField(max_length=50,null=True)
temp = models.CharField(max_length=50,null=True)
temp_2 = models.CharField(max_length=50,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,null=True)
test_panel = [
FieldPanel('test')
]
edit_handler = TabbedInterface([
ObjectList(test_panel,heading="Test")
])
我可以立即看到无法在此处注册设置的问题 - 它会直接转到“设置”。此外,它也不起作用。
同样,有问题的设置和模型在单独运行时都没有问题。我的问题是将它们聚合到一个子菜单中
这样做的最佳方法是什么?我曾尝试深入研究文档和 Stack Overflow,但没有找到答案。
谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)