Just to save page I do:
parent_page = Page.objects.get(title="parent")
page = Page(title="child", owner=request.user)
parent_page.add_child(instance=page)
page.save() # not sure if this required
What if I need to save Draft Page? This code seems to be working but I am not sure if it's correct:
parent_page = Page.objects.get(title="parent")
page = Page(title="child", live=False, owner=request.user)
parent_page.add_child(instance=page)
page.save_revision()
page.save()
Do I need call page.save() after page.save_revision()?
With this code everything works ok in my case but I have noticed that PageRevision.user_id is empty. How it can affect my code in future? From wagtail code I see that I can save revision with user parameter:
page.save_revision(user=request.user, log_action=True)
I have saved revisions without user and don't see any side effect. How this field is critical? Do I need to fill user_id field for all existing revisions?
P.S. looks like it would be better to wrap this code in transaction because when it fails due to validation error manage.py fixtree
show me error Incorrect numchild value
.