Is there a way to override the label and help text on a FieldPanel?

这一生的挚爱 提交于 2019-12-11 07:47:31

问题


I'd like to change the help text and the label for some of the Page fields in the admin interface. It seems like normally the FieldPanel picks up the label and help text from the model field, but since I'm wanting to change these values for fields on the Page model (title and search_description, specifically), I can't set verbose_name and help_text on the field itself

I tried passing in the heading and help_text keyword arguments to FieldPanel, but I'm still seeing the default label and help text in the admin interface.

class MyPage(Page):
    content_panels = [
        FieldPanel('title', heading='Name', classname='full'),
        FieldPanel('search_description', heading='Description',
                   classname='full',
                   help_text='Description used in indices and search results')
    ]

回答1:


There is a way to overwrite the default help_text and label (called verbose_name) on a per-field basis.

MyPage._meta.get_field("title").help_text = "Help me Obi-Wan, you're my only help_text"
MyPage._meta.get_field("title").verbose_name = "Jedi Labelling"

There's this method. And then there's also the MultiFieldPanel's heading which Dan Swain had covered nicely in his answer.

And if all that doesn't quite work, there's always the Wagtail HelpPanel. More on that in the docs at http://docs.wagtail.io/en/v2.6.1/reference/pages/panels.html#helppanel

I've also created a YouTube video around this subject if you prefer to learn from videos.

Hope this helps!




回答2:


I don't know of an easy way to add help text directly to an individual field, but you could wrap the fields in a MultiFieldPanel and put a heading on it:

class MyPage(Page):
    content_panels = [
        MultiFieldPanel([
            FieldPanel('title', heading='Name', classname='full'),
            FieldPanel('search_description', heading='Description',
                       classname='full',
                       help_text='Description used in indices and search results')
        ], heading="your help text")
    ]

In wagtail.core.models.py you will see:

from django.utils.translation import ugettext_lazy as _

class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase):
    title = models.CharField(
        verbose_name=_('title'),
        max_length=255,
        help_text=_("The page title as you'd like it to be seen by the public")
    )

Since Page is not an abstract class, you can't override its fields in your own Page-based classes even if you try to redefine title. Also, notice the import of ugettext_lazy as _ and then the _('title') in the verbose_name declaration. This answer explains that this code is getting the translated version of the title's verbose_name.




回答3:


A suggestion from @KalobTaulien in the Wagtail Slack that seems to do the trick:

class MyPage(Page):
    pass

MyPage._meta.get_field('title').verbose_name = 'name'
MyPage._meta.get_field('search_description').help_text = 'Description used in indices and search results'


来源:https://stackoverflow.com/questions/57482316/is-there-a-way-to-override-the-label-and-help-text-on-a-fieldpanel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!