Skip to main content
 首页 » 编程设计

python之如何在 wagtail 中正确使用 taggit

2025年04月02日17youxin

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

请您参考如下方法:

有一个在 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'), 
    ]