0

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

4

1 回答 1

0

您必须创建自己的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

于 2020-04-24T15:37:25.860 回答