如何在令人讨厌的情况下正确使用taggit

时间:2016-04-18 21:35:45

标签: python django wagtail

这是我的代码,但是我不确定为什么Taggable在尝试根据页面添加新内容时会给我一个错误。

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

    tags = TaggableManager()

这是我得到的错误。

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

1 个答案:

答案 0 :(得分:1)

在wagtail页面中使用标签的具体方法(http://docs.wagtail.io/en/v1.4.3/reference/pages/model_recipes.html?highlight=tags#tagging)。我在这里从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'),
    ]