Wagtail 自定义文档模型 [英] Wagtail Custom Document Model

查看:32
本文介绍了Wagtail 自定义文档模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这个帖子中了解到 https://github.com/wagtail/wagtail/issues/2001

I understand from this thread https://github.com/wagtail/wagtail/issues/2001

您可以自定义 Wagtail 文档模型,就像您可以自定义图像模型一样,为其添加额外的字段.我找不到任何有关如何执行此操作的文档.如果有人对从哪里开始有任何建议,那就太好了.我是 wagtail 的新手,因此非常感谢任何帮助.

that you can customise the Wagtail Document Model, as you can the Image Model, to add extra fields to it. What I can't find is any documentation on how to do this. If anyone has any suggestions on where to start that would be great. I'm new to wagtail so any help much appreciated.

谢谢!

推荐答案

自定义文档模型的实现方式与 自定义图像模型.要添加您自己的文档模型(我们称之为 CustomDocument),您应该执行以下操作:

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. 创建一个继承自 wagtail.wagtaildocs.models.AbstractDocument 的模型.应该是这样的:

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

  • 注册一个 post_delete 信号处理程序以在数据库中删除文档记录后从磁盘中删除文件.应该是这样的:

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

  • WAGTAILDOCS_DOCUMENT_MODEL 设置设置为指向您的模型.示例:

  • Set the WAGTAILDOCS_DOCUMENT_MODEL setting to point to your model. Example:

    `WAGTAILDOCS_DOCUMENT_MODEL = 'your_app_label.CustomDocument'` 
    

  • 这篇关于Wagtail 自定义文档模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆