问题
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?
回答1:
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')
]
Original source: https://github.com/wagtail/wagtail/issues/2893
来源:https://stackoverflow.com/questions/60361563/wagtail-how-to-set-calculated-fields-property-title-in-admin