gpt4 book ai didi

Wagtail 自定义文档模型

转载 作者:行者123 更新时间:2023-12-02 08:14:18 25 4
gpt4 key购买 nike

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

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

谢谢!

最佳答案

自定义文档模型可以用与 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'` 

关于Wagtail 自定义文档模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43388180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com