向 Wagtail 索引视图添加列标题(和相应的 属性)

Add column heading (and corresponding property) to Wagtail index view

我和我的团队创建了一个部分,允许我们公司添加登陆页面。我们希望在关联模型的索引视图中包含一些额外的列,如此图所示。

我发现一些旧帖子(2014 年左右)表明这是不可能的,但我找不到任何更新的内容使该声明无效。是否可以这样做,如果可以,有人可以指出正确的方向吗?

您可以使用 ModelAdmin for a specific model, but it won't appear in the Pages Explorer view as in your screenshot. Instead, it will appear in a new menu item on the left sidebar. I also find that Hooks is a great place to store this logic. Just be aware of how ModelAdmin differs from modeladmin. The Bakery Demo 来完成此操作,这里有一些很好的示例来说明这一切是如何工作的。

如果您愿意为页面浏览器修补视图和模板,您应该能够做到这一点。我的团队没有修补 Page Explorer,所以我没有相关的示例代码,但我们的一般方法如下:

  1. 我们有一个名为 wagtail_patches 的 django 应用程序,它列在我们的 INSTALLED_APPS wagtail 应用程序之前。
  2. 在 wagtail_patches/apps.py 中我们有:

    from django.apps import AppConfig
    
    class WagtailPatchesConfig(AppConfig):
        name = 'wagtail_patches'
        verbose_name = 'Wagtail Patches'
        ready_is_done = False
    
        def ready(self):
            """
            This function runs as soon as the app is loaded. It executes our monkey patches to various parts of Wagtail
            that change it to support our architecture of fully separated tenants.
            """
            # As suggested by the Django docs, we need to make absolutely certain that this code runs only once.
            if not self.ready_is_done:
                # The act of performing this import executes all the code in monkey_patches.
                from . import monkey_patches  
                # Unlike monkey_patches, the code of wagtail_hook_patches is in the function patch_hooks().
                from .wagtail_hook_patches import patch_hooks
                patch_hooks()
    
                self.ready_is_done = True
            else:
                print("{}.ready() executed more than once! This method's code is skipped on subsequent runs.".format(
                    self.__class__.__name__
                ))
    
  3. 然后在wagtail_patches/monkey_patches.py中我们导入要打补丁的模块,然后写一个新方法,然后用新方法替换stock版本。例如:

    from wagtail.admin.forms.collections import CollectionForm
    def collection_form_clean_name(self):
        if <weird custom condition>:
            raise ValidationError('some error message')
    CollectionForm.clean_name = collection_form_clean_name
    
  4. 覆盖模板就像普通的 django 模板覆盖一样,将您自定义版本的某些文件放在与其在 Wagtail 中的通常位置相匹配的文件夹层次结构中。