这是我的代码,我不确定为什么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.
最佳答案
在w页面(http://docs.wagtail.io/en/v1.4.3/reference/pages/model_recipes.html?highlight=tags#tagging)中有一个使用标签的特定方法。我要从w文档复制此处:
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'),
]
关于python - 如何在w中正确使用taggit,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36704720/