Search code examples
pythondjangowagtailheadless-cms

How do I headlessly create a page in Wagtail?


Is there any way I mimic the Wagtail behaviour of creating pages?

The only way that I can think of is to totally replicate the POST request sent by them upon clicking 'Publish' in the admin interface, but that would be just hideous.

I need this because I need to get a product list from an api, treat them and insert them in my DB. I would prefer to do this through wagtail so I do not drift away from my CMS

In a perfect world this would be a call to some api endpoint where I provide the needed fields in the body of the request.


Solution

  • You can use management command (or any other trigger) to do something like:

    from wagtail.core.models import Page
    
    from myapp.models import CustomPage
    
    
    parent_page = Page.objects.filter(slug="parent_page")[0] # get a suitable parent 
    
    page = CustomPage(
            title="Sample name",
            depth=4,
            path="Some random path",
     )
    
    parent_page.add_child(instance=page)