I am trying to programmatically create a PostPage
object. This class inherits from wagtail's Page
model:
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')
)
However, I am getting the following error:
ValidationError: {'path': ['This field cannot be blank.'], 'depth': ['This field cannot be null.']}
I need to create some dummy Page
objects, so I wonder how I can solve this problem.
I found some information that helped me solve this problem at Google Groups' wagtail support question 1 and question 2. Basically, I cannot create directly the Page
object, but I have to add it to another existing page as follows:
# 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()
This worked like a charm!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With