2

这是我在 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.
4

1 回答 1

1

在 wagtail 页面中使用标签有一个特定的配方(http://docs.wagtail.io/en/v1.4.3/reference/pages/model_recipes.html?highlight=tags#tagging)。我从 wagtail 文档复制到这里:

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'),
    ]
于 2016-04-18T21:53:11.980 回答