Wagtail Custom Document Model

China☆狼群 提交于 2019-12-06 10:07:43

Custom document model can be implemented in a similar way as a custom image model. To add your own document model (let's call it CustomDocument) you should do the following:

  1. Create a model that inherit from wagtail.wagtaildocs.models.AbstractDocument. Should be something like:

    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. Register a post_delete signal handler to remove a file from your disk once document record deleted in the database. It should be something like this:

    # 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. Set the WAGTAILDOCS_DOCUMENT_MODEL setting to point to your model. Example:

    `WAGTAILDOCS_DOCUMENT_MODEL = 'your_app_label.CustomDocument'` 
    
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!