Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Page object programmatically got error ValidationError path and depth fields cannot be blank/null

Tags:

django

wagtail

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.

like image 596
lmiguelvargasf Avatar asked Mar 29 '19 02:03

lmiguelvargasf


1 Answers

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!

like image 129
lmiguelvargasf Avatar answered Nov 03 '22 15:11

lmiguelvargasf