使用外键集编辑网站设置时生成KeyError

时间:2019-01-29 04:22:04

标签: wagtail

Django版本: 2.1.5

Python版本: 3.6.8

Wagtail版本: 2.4

我有一个模板,页脚中有四列链接。我已经建立了以下模型,该模型由BaseSetting对象和页脚链接对象组成,用于链接的每一列。页脚链接对象将每个ForeignKey对象都TemplateItems对象。

@register_setting
class TemplateItems(BaseSetting):
    page_banner = models.OneToOneField('wagtailimages.Image', null=True, blank=True, on_delete=models.SET_NULL, related_name='+',  help_text='Banner image that shows below menu on pages other than home page')
    footer_link_col1_header = models.CharField(max_length=25, default='', verbose_name='Footer Link Column 1 Header')
    footer_link_col2_header = models.CharField(max_length=25, blank=True, default='', verbose_name='Footer Link Column 2 Header')
    footer_link_col3_header = models.CharField(max_length=25, blank=True, default='', verbose_name='Footer Link Column 3 Header')
    footer_link_col4_header = models.CharField(max_length=25, blank=True, default='', verbose_name='Footer Link Column 4 Header')

    panels = [
        ImageChooserPanel('page_banner'),
        MultiFieldPanel([
            FieldPanel('footer_link_col1_header'),
            InlinePanel('footer_links_col_1', label='Column 1 Links'),
            FieldPanel('footer_link_col2_header'),
            InlinePanel('footer_links_col_2', label='Column 2 Links'),
            FieldPanel('footer_link_col3_header'),
            InlinePanel('footer_links_col_3', label='Column 3 Links'),
            FieldPanel('footer_link_col4_header'),
            InlinePanel('footer_links_col_4', label='Column 4 Links'),
        ], heading='Footer Links'),
        InlinePanel('social_media_links', label="Social Media Links"),
    ]


class FooterLink(Orderable):
    name = models.CharField(max_length=60, default='')
    url = models.CharField(max_length=200, default='')

    panels = [
        FieldRowPanel([
            FieldPanel('name'),
            FieldPanel('url'),
        ])
    ]

    class Meta:
        abstract = True

    def __str__(self):
        return f'{self.name}'


class FooterLinkCol1(FooterLink):
    template_items = ForeignKey('TemplateItems', related_name='footer_links_col_1', null=True, on_delete=models.SET_NULL)


class FooterLinkCol2(FooterLink):
    template_items = ForeignKey('TemplateItems', related_name='footer_links_col_2', null=True, on_delete=models.SET_NULL)


class FooterLinkCol3(FooterLink):
    template_items = ForeignKey('TemplateItems', related_name='footer_links_col_3', null=True, on_delete=models.SET_NULL)


class FooterLinkCol4(FooterLink):
    template_items = ForeignKey('TemplateItems', related_name='footer_links_col_4', null=True, on_delete=models.SET_NULL)

迁移已成功创建和迁移,但是当我在Wagtail管理员中转到TemplateItems设置对象以添加页脚链接时,出现以下错误:

  / p / admin / settings / main / templateitems / 2 /

中的

KeyError      

'footer_links_col_1'

如果我注释掉任何footer_links_col_X项目,那么我收到第一个未注释掉的错误。数据库中没有任何列的现有页脚链接。我想知道问题是否由于ForeignKeyBaseSetting对象而来,但是当我在Django admin中声明这些模型(包括每个列链接的内联)时,它显示并允许我可以添加链接。

跟踪:

  

文件   “ /opt/virtualenvs/MY_SITE-a0hNfZxl/lib/python3.6/site-packages/django/core/handlers/exception.py”   在内部     34. response = get_response(request)

     

文件   “ /opt/virtualenvs/MY_SITE-a0hNfZxl/lib/python3.6/site-packages/django/core/handlers/base.py”   在_get_response中     126. response = self.process_exception_by_middleware(e,request)

     

文件   “ /opt/virtualenvs/MY_SITE-a0hNfZxl/lib/python3.6/site-packages/django/core/handlers/base.py”   在_get_response中     124. response = wrapd_callback(request,* callback_args,** callback_kwargs)

     

文件   “ /opt/virtualenvs/MY_SITE-a0hNfZxl/lib/python3.6/site-packages/django/views/decorators/cache.py”   在_wrapped_view_func中     44. response = view_func(request,* args,** kwargs)

     

文件   “ /opt/virtualenvs/MY_SITE-a0hNfZxl/lib/python3.6/site-packages/wagtail/admin/urls/init.py”   在包装     102. return view_func(request,* args,** kwargs)

     

文件   “ /opt/virtualenvs/MY_SITE-a0hNfZxl/lib/python3.6/site-packages/wagtail/admin/decorators.py”   在decorated_view中     34. return view_func(request,* args,** kwargs)

     

文件   “ /opt/virtualenvs/MY_SITE-a0hNfZxl/lib/python3.6/site-packages/wagtail/contrib/settings/views.py”   编辑中     83. instance = instance,form = form,request = request)

     

文件   “ /opt/virtualenvs/MY_SITE-a0hNfZxl/lib/python3.6/site-packages/wagtail/admin/edit_handlers.py”   在bind_to_instance     153. new.on_instance_bound()

     

文件   “ /opt/virtualenvs/MY_SITE-a0hNfZxl/lib/python3.6/site-packages/wagtail/admin/edit_handlers.py”   在on_instance_bound     295. request = self.request))

     

文件   “ /opt/virtualenvs/MY_SITE-a0hNfZxl/lib/python3.6/site-packages/wagtail/admin/edit_handlers.py”   在bind_to_instance     153. new.on_instance_bound()

     

文件   “ /opt/virtualenvs/MY_SITE-a0hNfZxl/lib/python3.6/site-packages/wagtail/admin/edit_handlers.py”   在on_instance_bound     295. request = self.request))

     

文件   “ /opt/virtualenvs/MY_SITE-a0hNfZxl/lib/python3.6/site-packages/wagtail/admin/edit_handlers.py”   在bind_to_instance     153. new.on_instance_bound()

     

文件   “ /opt/virtualenvs/MY_SITE-a0hNfZxl/lib/python3.6/site-packages/wagtail/admin/edit_handlers.py”   在on_instance_bound     692. self.formset = self.form.formsets [self.relation_name]

     

异常类型:// admin / settings / main / templateitems / 2 /中的KeyError   异常值:“ footer_links_col_1”

1 个答案:

答案 0 :(得分:2)

InlinePanel要求相应的外键为ParentalKey

from modelcluster.fields import ParentalKey

class FooterLinkCol1(FooterLink):
    template_items = ParentalKey('TemplateItems', related_name='footer_links_col_1', null=True, on_delete=models.SET_NULL)

反过来,ParentalKey要求父模型从ClusterableModel继承(对于Wagtail Page模型自动为真):

from modelcluster.models import ClusterableModel

class TemplateItems(BaseSetting, ClusterableModel):

the readme for django-modelcluster中有一些关于ClusterableModel / ParentalKey动机的解释。)