如何在保存前更改 wagtail cms 页面标签
how to i can change wagtail cms page tag before save
如何在保存前更改 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()
但我不知道如何更改标签列表。
我找到了答案: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().save(*args, **kwargs)
(或者,在使用旧的 Python 2.7 时,super(ProductPage, self).save(*args, **kwargs)
)
如何在保存前更改 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()
但我不知道如何更改标签列表。
我找到了答案: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().save(*args, **kwargs)
(或者,在使用旧的 Python 2.7 时,super(ProductPage, self).save(*args, **kwargs)
)