Wagtail:如何在管理员中设置计算字段(@property)标题 [英] Wagtail: how to set calculated fields (@property) title in admin

查看:29
本文介绍了Wagtail:如何在管理员中设置计算字段(@property)标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use ModelAdmin module for my models in Wagtail. I have @property fields in models, where I return some annotated data and display it Index and Inspect Views in Admin. But Wagtail set title of such fields as field name in model. In regular field I use verbose_name to set nice title, how can I change titles for property field?

推荐答案

您必须创建自己的 ReadOnlyPanel,因为 Wagtail 无法实现.

You have to create your own ReadOnlyPanel as it is not possible with 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())

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

And to use it just import and use in your model:

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

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

这篇关于Wagtail:如何在管理员中设置计算字段(@property)标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆