如何在WagtailAdminPageForm中获取未保存页面实例的页面祖先?

时间:2019-06-24 21:21:55

标签: wagtail

我想在customized admin form中获得当前页面的祖先页面:

# models.py
class TopicPage(Page):

    def get_topic_path(self):
        return (
            self
            .get_ancestors(inclusive=True)
            .filter(depth__gt=3)
            .values_list('title', flat=True)
        )

    base_form_class = TopicPageForm

# forms.py
class TopicPageForm(WagtailAdminPageForm):

    def save(self, commit=True):
        page = super().save(commit=False)

        page.get_topic_path()

        if commit:
            page.save()
        return page

但是对于新的TopicPage实例,get_ancestors在保存页面之前不会返回任何内容:

  1. 添加新的TopicPage对象
  2. 发布此页面:get_ancestors返回空查询集
  3. 再次发布此页面:get_ancestors返回预期的查询集

即使对于新的(未保存的)页面,我如何也可以通过WagtailAdminPageForm save()方法获取当前页面的祖先页面?

1 个答案:

答案 0 :(得分:0)

通过Wagtail的页面添加/编辑视图实例化XxxxCloudFrontDistribution: Type: AWS::CloudFront::Distribution Properties: DistributionConfig: Aliases: - ${self:provider.environment.CUSTOM_DOMAIN} Origins: - DomainName: ${self:provider.environment.BUCKET_NAME}.s3-website-us-east-1.amazonaws.com Id: Xxxx CustomOriginConfig: HTTPPort: 80 OriginProtocolPolicy: http-only Enabled: 'true' DefaultRootObject: index.html CustomErrorResponses: - ErrorCode: 404 ResponseCode: 200 ResponsePagePath: /error.html DefaultCacheBehavior: AllowedMethods: - DELETE - GET - HEAD - OPTIONS - PATCH - POST - PUT TargetOriginId: Xxxx Compress: 'true' ForwardedValues: QueryString: 'false' Cookies: Forward: none ViewerProtocolPolicy: redirect-to-https ViewerCertificate: AcmCertificateArn: ${self:provider.environment.ACM_CERT_ARN} SslSupportMethod: sni-only 时,它将传递给父页面;然后可以在表单对象上以WagtailAdminPageForm的形式使用它。 Wagtail使用此方法来验证页面信息的唯一性,但是您也可以在自定义self.parent_page方法中使用它-在这种情况下,您可以通过在父方法上调用save来复制get_topic_path页,并将.get_ancestors(inclusive=True)附加到结果。