我已將自定義頁面模型(博客文章)定義為父模型(博客索引頁面)的子模型,并且我想測驗是否可以在其父模型下創建子模型。
BlogPage 和 BlogIndexPage 模型是從檔案中的 wagtail “基本博客”示例復制而來的,并且按預期作業。
我正在嘗試遵循檔案,但出現以下驗證錯誤:
AssertionError: Validation errors found when creating a cms.blogpage:
E date:
E This field is required.
E intro:
E This field is required.
E slug:
E This field is required.
E title:
E This field is required.
我懷疑我錯誤地定義了我的夾具,但我不是正確的形式。任何幫助是極大的贊賞!有人可以解釋為什么它不起作用嗎?
夾具(apps.cms.tests.fixtures.blogPage.json):
[
{
"model":"wagtailcore.page",
"pk": 1,
"fields":{
"date":"2022-02-28",
"intro":"intro to the post...",
"slug":"slug/",
"title":"the title",
"body":"body of the post...",
"categories":[
1
],
"content_type": ["cms", "blogpage"],
"depth": 2
}
},
{
"model": "cms.blogpage",
"pk": 1,
"fields": {}
}
]
測驗類(apps.cms.tests.test_pages.py):
class MyPageTests(WagtailPageTests):
def setUp(self):
self.login()
page = BlogIndexPage(title="Home page", slug="home", path="foo", depth=1)
page.save()
def test_create_blog_post(self):
cwd = Path.cwd()
root_page = BlogIndexPage.objects.first()
with open(f"{cwd}/lettergun/apps/cms/tests/fixtures/BlogPage.json") as json_file:
fixture = json.load(json_file)
# Assert that a ContentPage can be made here, with this POST data
self.assertCanCreate(root_page, BlogPage, nested_form_data(fixture))
模型(apps.cms.models.py):
class BlogIndexPage(Page):
template = "blog.html"
intro = models.TextField(blank=True)
def get_context(self, request):
# Update context to include only published posts, ordered by reverse-chron
context = super().get_context(request)
blogpages = self.get_children().live().order_by("-first_published_at")
context["blogpages"] = blogpages
return context
content_panels = Page.content_panels [FieldPanel("intro", classname="full")]
class BlogPageTag(TaggedItemBase):
content_object = ParentalKey("BlogPage", related_name="tagged_items", on_delete=models.CASCADE)
class BlogPage(Page):
template = "blog-post.html"
date = models.DateField("Post date")
intro = models.CharField(max_length=250)
body = RichTextField(blank=True)
tags = ClusterTaggableManager(through=BlogPageTag, blank=True)
categories = ParentalManyToManyField("cms.BlogCategory", blank=True)
def main_image(self):
gallery_item = self.gallery_images.first()
if gallery_item:
return gallery_item.image
else:
return None
search_fields = Page.search_fields [
index.SearchField("intro"),
index.SearchField("body"),
]
content_panels = Page.content_panels [
MultiFieldPanel(
[
FieldPanel("date"),
FieldPanel("tags"),
FieldPanel("categories", widget=forms.CheckboxSelectMultiple),
],
heading="Blog information",
),
FieldPanel("intro"),
FieldPanel("body"),
InlinePanel("gallery_images", label="Gallery images"),
]
uj5u.com熱心網友回復:
的最后一個引數self.assertCanCreate是要提交到“創建頁面”管理視圖的 HTTP POST 資料字典。這與夾具(它是存盤在資料庫中的頁面資料的表示)完全不同,并且資料結構不兼容。
在最簡單的情況下,POST 字典可以只包含必填欄位date、intro和:slugtitle
self.assertCanCreate(root_page, BlogPage, {
'date': '2022-02-28',
'intro': "intro to the post...",
'slug': 'my-blog-page',
'title': 'My blog page',
})
僅當您的測驗正在創建一個包含比欄位串列更復雜的資料的頁面時才需要該由于您的頁面上有一個 InlinePanel,即使您沒有向其傳遞任何資料,您也需要考慮這一點 - 有關詳細資訊,請參閱https://stackoverflow.com/a/71356332/1853523。nested_form_data幫助程式 - 例如,如果您希望您的頁面資料包含一些畫廊影像,您需要將它與inline_formset幫助程式一起使用.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/441644.html
