我正在尝试以编程方式创建一个PostPage对象。这个类继承自 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')
)
Run Code Online (Sandbox Code Playgroud)
但是,我收到以下错误:
ValidationError: {'path': ['This field cannot be blank.'], 'depth': ['This field cannot be null.']}
我需要创建一些虚拟Page对象,所以我想知道如何解决这个问题。
我在 Google Groups 的 wagtail support question 1 和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()
Run Code Online (Sandbox Code Playgroud)
这就像一个魅力!
| 归档时间: |
|
| 查看次数: |
854 次 |
| 最近记录: |