以编程方式创建页面对象时出现错误 ValidationError 路径和深度字段不能 blank/null
Creating Page object programmatically got error ValidationError path and depth fields cannot be blank/null
我正在尝试以编程方式创建一个 PostPage
对象。此 class 继承自 wagtail 的 Page
模型:
post = PostPage.objects.create(
title='Dummy',
intro='This is just for testing dummy',
body='Lorem ipsum dolor...',
first_published_at=datetime.strptime(f'2019-01-01', '%Y-%m-%d')
)
但是,我收到以下错误:
ValidationError: {'path': ['This field cannot be blank.'], 'depth': ['This field cannot be null.']}
我需要创建一些虚拟 Page
对象,所以我想知道如何解决这个问题。
我在 Google Groups 的 wagtail 支持 question 1 and question 2 找到了一些帮助我解决这个问题的信息。基本上,我不能直接创建 Page
对象,但我必须将它添加到另一个现有页面,如下所示:
# assuming HomePage has at least one element
home = HomePage.objects.all()[0]
post = PostPage(
title='Dummy',
intro='This is just for testing dummy',
body='Lorem ipsum dolor...',
first_published_at=datetime.strptime(f'2019-01-01', '%Y-%m-%d'),
)
home.add_child(instance=post)
home.save()
这太棒了!
我正在尝试以编程方式创建一个 PostPage
对象。此 class 继承自 wagtail 的 Page
模型:
post = PostPage.objects.create(
title='Dummy',
intro='This is just for testing dummy',
body='Lorem ipsum dolor...',
first_published_at=datetime.strptime(f'2019-01-01', '%Y-%m-%d')
)
但是,我收到以下错误:
ValidationError: {'path': ['This field cannot be blank.'], 'depth': ['This field cannot be null.']}
我需要创建一些虚拟 Page
对象,所以我想知道如何解决这个问题。
我在 Google Groups 的 wagtail 支持 question 1 and question 2 找到了一些帮助我解决这个问题的信息。基本上,我不能直接创建 Page
对象,但我必须将它添加到另一个现有页面,如下所示:
# assuming HomePage has at least one element
home = HomePage.objects.all()[0]
post = PostPage(
title='Dummy',
intro='This is just for testing dummy',
body='Lorem ipsum dolor...',
first_published_at=datetime.strptime(f'2019-01-01', '%Y-%m-%d'),
)
home.add_child(instance=post)
home.save()
这太棒了!