djangowagtaildjango-treebeard

wagtail: Adding page as the first child of the parent page on creation


Im trying to add the created list page as the first child of the parent Index page. The problem is that when I use django treebeard api django treebeard api it succeeds sometimes but other times it shows me an error:

{'path': ['Page with this Path already exists.']}

How can I solve this?

CODE:

@receiver(post_save, sender=ListPage)
def move_page(sender, instance, **kwargs):
    instance.move(instance.get_parent(), pos='first-child')

Solution

  • TreeBeard API Solution: you just need to changethe target node by a sibling or child of a parent node, not the parent node itself.

    Pythonic Solution: You Can swap the page ID (Primary Key), first, create your page, then create a function to check all IDs and indicate your newly created page ID, then change the lowest ID number to be equal to the last child(which is your newly created page) + 1 and then change your page ID to the 1 or 0..!

    NOTE: if you need to rearrange the entire pages, you better use --fix-tree management command in the terminal(like: python manage.py fixtree), and then go for steps mentioned above, you can also create a recursion function to do all the above at once..! I'll intentionally leave the chance for other developers to help you with recursion functions example(unless you can do it by yourself)..!