在 Wagtail 中测试相关页面

Testing related page in Wagtail

我有一个 ContentPage 鹡鸰模型和一个将其他 ContentPage 模型链接到 ContentPageRelatedPost 模型,有点像这样:

class ContentPage(Page):
    summary = RichTextField(blank=True)
    body = RichTextField(blank=True)
    published = models.DateTimeField(default=timezone.now())

    content_panels = Page.content_panels + [
        FieldPanel('summary'),
        FieldPanel('body', classname="full"),
        InlinePanel('related_page', label="Related Content"),
    ]

    settings_panels = Page.settings_panels + [
        FieldPanel('published'),
    ]

class RelatedPost(Orderable):
    post = ParentalKey(
        'ContentPage',
        related_name='related_page'
    )
    page = models.ForeignKey(
        'ContentPage',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="+"
    )
    panels = [
        FieldPanel('page')
    ]

当我运行这个测试时:

class ContentPageTests(WagtailPageTests):
    def test_can_create_article_page(self):
        self.assertCanCreateAt(ContentIndexPage, ContentPage)

        # content_index is just a parent page
        content_index = self.create_content_index_page()
        self.assertCanCreate(content_index, ContentPage, {
            'title': 'Test Article',
            'published': datetime.datetime.now()
        })

我收到一条错误消息:

django.core.exceptions.ValidationError: ['ManagementForm data is missing or has been tampered with']

管理员工作正常。我可以保存相关页面等,当我注释掉 InlinePanel 行时它工作正常。

传递给 assertCanCreate 的数据需要与提交到 Wagtail 管理中的 'edit page' 表单的表单提交格式相匹配。对于 InlinePanel 中的子模型,Wagtail 使用 Django 表单集处理此问题 - 请参阅 https://docs.djangoproject.com/en/1.10/topics/forms/formsets/#formset-validation - and so you need to supply all the fields that the Django formset logic would expect, including the management form。最简单的通过验证的情况是一个管理表单,它只是报告没有子表单:

   self.assertCanCreate(content_index, ContentPage, {
        'title': 'Test Article',
        'published': datetime.datetime.now(),
        'related_page-TOTAL_FORMS': 0,
        'related_page-INITIAL_FORMS': 0,
        'related_page-MAX_NUM_FORMS': 999,
    })