问题
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