【问题标题】:Wagtail Custom Document Model鹡鸰自定义文档模型
【发布时间】:2017-04-13 09:16:02
【问题描述】:

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

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

谢谢!

【问题讨论】:

    标签: wagtail


    【解决方案1】:

    自定义文档模型的实现方式与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'` 
      

    【讨论】:

    • 太好了,谢谢您-感谢您的快速回复:-)
    • 如果这解决了您的问题,您应该选择 m1kola 对您的问题@gbmillard 的回答。这样他们就会得到奖励,其他人浏览这个页面会更容易。
    【解决方案2】:

    对@m1kola 的回答稍作修正:您不需要注册 post_delete 信号。鹡鸰已经做到了:

    def post_delete_file_cleanup(instance, **kwargs):
        # Pass false so FileField doesn't save the model.
        transaction.on_commit(lambda: instance.file.delete(False))
    
    
    def register_signal_handlers():
        # in example above it returns CustomDocument model and register signal 
        Document = get_document_model() 
        post_delete.connect(post_delete_file_cleanup, sender=Document)
    

    查看他们的github

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-08
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 2019-03-13
      • 2018-08-28
      • 1970-01-01
      相关资源
      最近更新 更多