如何在保存之前更改wagtail cms页面标记

时间:2017-04-14 23:06:18

标签: python django wagtail

如何在保存之前更改wagtail页面标记?

我可以通过覆盖save()来改变标题 -

class ProductPageTag(TaggedItemBase):
    content_object = ParentalKey('product.ProductPage',related_name='tagged_items')

class ProductPage(Page):
    body = StreamField(BodyStreamBlock)
    tags = ClusterTaggableManager(through=ProductPageTag, blank=True)
    def save(self, *args, **kwargs):
      self.title = "my title" # work
      self.tags = "test,test2,test3" #not work
      super(ProductPage, self).save()

但我不知道如何更改标签列表。

1 个答案:

答案 0 :(得分:2)

我找到了答案:D

只需要改变

self.tags = "test,test2,test3"

self.tags.add('test',"test2","test3")

最终代码

class ProductPageTag(TaggedItemBase):
    content_object =ParentalKey('product.ProductPage',related_name='tagged_items')

class ProductPage(Page):
    body = StreamField(BodyStreamBlock)
    tags = ClusterTaggableManager(through=ProductPageTag, blank=True)

    def save(self, *args, **kwargs):
      self.title = "my title" # work
      self.tags.add('test',"test2","test3") #work
      super(ProductPage, self).save()