如何在Django Wagtail中的子对象中重用父页面字段?

时间:2018-11-12 09:24:29

标签: python django wagtail

我正在尝试重用Django Wagtail中的某些字段。

我的主主页HomePage如下:

class HomePage(Page):
    # Footer section #
    footer_address = models.CharField(max_length=50, blank=False, null=False, default='My company address')
    footer_phone = models.CharField(max_length=50, blank=False, null=False, default='My company phone')
    footer_email = models.CharField(max_length=50, blank=False, null=False, default='My company email')
    footer_website = models.CharField(max_length=50, blank=False, null=False, default='My company website')
    # End Footer section #

    def get_context(self, request):
        context = super().get_context(request)
        context['logo'] = self.logo
        context['address'] = self.footer_address
        context['phone'] = self.footer_phone
        context['email'] = self.footer_email
        context['website'] = self.footer_website
        return context

    content_panels = Page.content_panels + [
        FieldRowPanel([FieldPanel('footer_address', classname="col3 footer_address"), FieldPanel('footer_phone', classname="col3 footer_phone"),
                       FieldPanel('footer_email', classname="col3 footer_email"), FieldPanel('footer_website', classname="col3 footer_website")], heading="Footer contact info",
                      classname="full g-py-30 footer"),
    ]

我可以在其中添加管理面板地址,电话,电子邮件和网站。很好。

但是我希望能够在Vehicles的子页面(HomePage)中编辑这些字段。

get_context中,我得到的HomePageHomePage.objects.parent_of(self).live().first(),我可以在子页面的模板中传递这些字段的值,但是如何从子级的父级中编辑或添加它们的值页面?

代码如下:

class Vehicles(Page):
    vehicles_list_title = models.CharField(max_length=100, blank=False, null=False, default=_('Discover vehicles that suit you'), verbose_name=_("Title"))

    def get_context(self, request):
        # Update context to include only published posts, ordered by reverse-chron
        context = super().get_context(request)
        homepage_parent = HomePage.objects.parent_of(self).live().first()
        context['logo'] = homepage_parent.logo
        context['address'] = homepage_parent.footer_address
        context['phone'] = homepage_parent.footer_phone
        context['email'] = homepage_parent.footer_email
        context['website'] = homepage_parent.footer_website
        return context

    content_panels = Page.content_panels + [
        #### What I need to do here to show and to be able to edit fields from parent page

    ]

有什么主意吗?

0 个答案:

没有答案