6

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.

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228

1 Answers1

8

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!

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228