I managed to programatically create pages using Django Management Commands as shown here and here.
How do I link those pages to other models like tags, categories and authors?
Here is the model of the page I want to create:
class BlogPage(Page):
## Fields I can create programatically
date = models.DateField("Post date")
intro = models.CharField(max_length=250)
body = RichTextField(blank=True)
## Fields I dont know what to do with
tags = ClusterTaggableManager(through='BlogPageTag', blank=True)
categories = ParentalManyToManyField('blog.BlogCategory', blank=True)
authors = ParentalManyToManyField('blog.BlogAuthor', blank=True)
def main_image(self):
gallery_item = self.gallery_images.first()
if gallery_item:
return gallery_item.image
else:
return None
Here is the code that I use to create the page without tags, authors and categories:
from django.core.management.base import BaseCommand
from wagtail.models import Page
from blog.models import BlogIndexPage, BlogPage
class Command(BaseCommand):
help = 'Create new Blog Page'
def handle(self, *args, **options):
home_page = Page.objects.type(BlogIndexPage).first()
new_page = BlogPage(
title="My Page",
slug="mypage",
date = "2022-07-17",
intro = "Some Intro",
body = "Some Body",
)
home_page.add_child(instance=new_page)
new_page.save_revision().publish()