how to i can change wagtail cms page tag before save

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 07:07:50

问题


How can I change wagtail page tag before saving?

I can change the title by overriding save() like this-

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()

but I don't know how to change the tag list.


回答1:


I Found the Answer :D

just need change

self.tags = "test,test2,test3"

to

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

final code

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()


来源:https://stackoverflow.com/questions/43420333/how-to-i-can-change-wagtail-cms-page-tag-before-save

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!