How can I programmatically save Wagtail Pages With Orderable Objects

Viewed 188

I have the model:

class HomePage(Page):
    content_panels = Page.content_panels + [
        InlinePanel('homepage_content', label="Homepage Content")
    ]
    template = 'home.html'

I have the Orderable object:

class HomePageContent(Orderable):
    page = ParentalKey(HomePage, on_delete=models.SET_NULL, related_name='homepage_content', null=True)
    principal_welcome_message = models.TextField()
    the_way_we_teach = RichTextField()
    meet_our_teachers = RichTextField()
    meet_our_students = RichTextField()

    # Editor panels configuration

    panels = [
        FieldPanel('principal_welcome_message'),
        FieldPanel('the_way_we_teach'),
        FieldPanel('meet_our_teachers'),
        FieldPanel('meet_our_students'),
    ]

This is how I save a HomePage object [programmatically]:

root = Page.objects.get(title="Root").specific

school_home_page = HomePage(title="Home Page")
root.add_child(instance=school_home_page).save_revision().publish()

site = Site.objects.first():

site.root_page = school_home_page
site.save()

This is how I save the HomePageContent object [programmatically]:

home_page_content = HomePageContent(
    page=school_home_page,
    principal_welcome_message="Default statement",
    the_way_we_teach="Default Tech",
    meet_our_teachers="Default developers",
    meet_our_students="Default analysts"
)

With this setup above, I cannot get the HomePageContent object to be saved along side the HomePage object. I have also tried adding the HomePageContent as a child object to the HomePage object, still no progress...

If you know any [standard] way to save Orderable objects associated with Page objects via ParentalKey alongside the corresponding Page object [programmatically and not via the admin interface], your help is greatly needed. Thank you.

0 Answers
Related