Home > front end >  Create pages programaticallhy with Django and Wagtail
Create pages programaticallhy with Django and Wagtail

Time:07-26

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()

CodePudding user response:

Because you have used ParentalManyToManyFields, you can add categories and authors to the new_page before saving it for the first time.

    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",           
        )
        category, created = BlogCategory.objects.get_or_create(name="Category 1")
        new_page.categories = [category]

        home_page.add_child(instance=new_page)

        new_page.save_revision().publish()

I don't have a tagging example handy. Because it goes through TaggableManager, you may have to use add or set from https://django-taggit.readthedocs.io/en/latest/api.html

CodePudding user response:

Managed to get it working based on the answer from cnk:

    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",           
        )
        
        category, created = BlogCategory.objects.get_or_create(name="MYCATEGORY")
        new_page.categories = [category]
        
        author, created = BlogAuthor.objects.get_or_create(name="MYAUTHOR")
        new_page.authors = [author]
        
        new_page.tags.add('MYTAG')
        
        home_page.add_child(instance=new_page)
        new_page.save_revision().publish()
  • Related