Reputation: 81
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.
Upvotes: 0
Views: 1481
Reputation: 81
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().save(*args, **kwargs)
(Or, when working with the old Python 2.7, super(ProductPage, self).save(*args, **kwargs)
)
Upvotes: 4