Rockink
Rockink

Reputation: 180

How to properly make taggit use in wagtail

Here is my code in wagtail, and I am not sure why Taggable gives me an error when trying to add new content based on the page.

class BlogPage(Page):
    body = StreamField([
        ('heading', blocks.CharBlock(classname="full title")),
        ('paragraph', blocks.RichTextBlock()),
        ('image', ImageChooserBlock()),
        ('python', TextBlock()),
    ])

    tags = TaggableManager()

This is the error that I get.

BlogPage objects need to have a primary key value before you can access their tags.

Upvotes: 2

Views: 1808

Answers (1)

Serafeim
Serafeim

Reputation: 15104

There's a specific recipe for using tags in wagtail pages (http://docs.wagtail.io/en/v1.4.3/reference/pages/model_recipes.html?highlight=tags#tagging). I'm copying here from the wagtail docs:

from modelcluster.fields import ParentalKey
from modelcluster.contrib.taggit import ClusterTaggableManager
from taggit.models import TaggedItemBase

class BlogPageTag(TaggedItemBase):
    content_object = ParentalKey('demo.BlogPage', related_name='tagged_items')

class BlogPage(Page):
    ...
    tags = ClusterTaggableManager(through=BlogPageTag, blank=True)

    promote_panels = Page.promote_panels + [
        ...
        FieldPanel('tags'),
    ]

Upvotes: 1

Related Questions