2017-04-13 62 views

回答

4

自定義文檔模型可以採用與custom image model類似的方式實現。要添加自己的文檔模型(我們稱之爲CustomDocument),你應該做到以下幾點:

  1. 創建從wagtail.wagtaildocs.models.AbstractDocument繼承的典範。應該是這樣的:

    class CustomDocument(AbstractDocument): 
        # Add your custom model fields here 
    
        admin_form_fields = (
         'title', 
         'file', 
         'collection', 
         'tags' 
         # Add your custom model fields into this list, 
         # if you want to display them in the Wagtail admin UI. 
        ) 
    
  2. 註冊一個post_delete信號處理程序,以從磁盤中刪除文件,一旦文件記錄在數據庫中刪除。它應該是這樣的:

    # Receive the post_delete signal and delete the file associated with the model instance. 
    @receiver(post_delete, sender=CustomDocument) 
    def document_delete(sender, instance, **kwargs): 
        # Pass false so FileField doesn't save the model. 
        instance.file.delete(False) 
    
  3. 設置WAGTAILDOCS_DOCUMENT_MODEL設置爲指向你的模型。例如:

    `WAGTAILDOCS_DOCUMENT_MODEL = 'your_app_label.CustomDocument'` 
    
+0

那太好了,謝謝你 - 感謝您的迅速反應:-) – gbmillard