2

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.

1 Answers1

2

You don't need page.save() in either case - parent_page.add_child handles the saving. (This might be what's causing the manage.py fixtree issue - django-treebeard doesn't update in-memory instances after database operations, so fields like numchild may be getting saved with incorrect values.)

The user field on revisions is optional, because changes to pages are not always performed by users - they might be done programmatically, like you are doing. It's just there for logging purposes (e.g. to show in the revision history of a page in the Wagtail admin).

gasman
  • 23,691
  • 1
  • 38
  • 56
  • Thank you for your answer. I don't need history for those pages so can I assume then that I don't need revision at all? I mean is it safe to create draft page without revision? Also as I said my pure experience with this code tells me to wrap code into transaction. It could be easily done in django with: decorator `@transaction.atomic` or context manager `with transaction.atomic()`. – Igor Margitich Apr 10 '21 at 08:28
  • No, you don't need revision objects - the editing interface will fall back on editing the 'real' page object if none are found. – gasman Apr 11 '21 at 09:12