2017-04-14 40 views
0

我如何保存之前更改鶺鴒頁面標籤?如何我可以改變鶺鴒CMS頁面標籤之前保存

我可以通過重寫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

你能不能顯示完整的頁面d請標籤類? – dentemm

+0

好吧,我加入到這個問題 –

+0

如果你想保存一個對象之前,做任何事情,你可以用'signals'爲你的情況可能是'presave'檢查出的Django的文檔[1]:https://開頭的文檔。 djangoproject.com/en/1.10/ref/signals/#django.db.models.signals.pre_save –

回答

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