【问题标题】:Wagtail: how to set calculated fields (@property) title in adminWagtail:如何在管理员中设置计算字段(@property)标题
【发布时间】:2020-06-07 05:39:39
【问题描述】:

我在 Wagtail 中为我的模型使用 ModelAdmin 模块。 我在模型中有@property 字段,我在其中返回一些带注释的数据并在管理员中显示它的索引和检查视图。但是 Wagtail 在模型中将这些字段的标题设置为字段名称。在常规字段中,我使用 verbose_name 设置好标题,如何更改属性字段的标题?

【问题讨论】:

    标签: wagtail modeladmin


    【解决方案1】:

    您必须创建自己的 ReadOnlyPanel,因为 Wagtail 无法做到这一点。

    mypanel.py

    from django.forms.utils import pretty_name
    from django.utils.html import format_html
    from django.utils.translation import ugettext_lazy as _
    from wagtail.admin.edit_handlers import EditHandler
    
    class ReadOnlyPanel(EditHandler):
        def __init__(self, attr, *args, **kwargs):
            self.attr = attr
            super().__init__(*args, **kwargs)
    
        def clone(self):
            return self.__class__(
                attr=self.attr,
                heading=self.heading,
                classname=self.classname,
                help_text=self.help_text,
            )
    
        def render(self):
            value = getattr(self.instance, self.attr)
            if callable(value):
                value = value()
            return format_html('<div style="padding-top: 1.2em;">{}</div>', value)
    
        def render_as_object(self):
            return format_html(
                '<fieldset><legend>{}</legend>'
                '<ul class="fields"><li><div class="field">{}</div></li></ul>'
                '</fieldset>',
                self.heading, self.render())
    
        def render_as_field(self):
            return format_html(
                '<div class="field">'
                '<label>{}{}</label>'
                '<div class="field-content">{}</div>'
                '</div>',
                self.heading, _(':'), self.render())
    

    要使用它,只需在您的模型中导入和使用:

    from .mypanel import ReadOnlyPanel
    class YourPage(Page):
        content_panels = Page.content_panels + [
            ReadOnlyPanel('myproperty', heading='Parent')
        ]
    

    原文来源:https://github.com/wagtail/wagtail/issues/2893

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-06
      • 2019-10-22
      • 1970-01-01
      相关资源
      最近更新 更多