4

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')
    ]
darth_mall
  • 984
  • 8
  • 16

3 Answers3

9

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"

enter image description here

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!

Kalob Taulien
  • 1,817
  • 17
  • 22
  • Oddly, this only seems to work for one class. e.g. if I do this for the title field's `help_text` on one class it works fine. If I then do it for the title field's `help_text` for a different page class further down the same file, then the the first class uses the help text defined for the second! Yes, I've checked I've used the correct class names. – Phil Gyford Feb 10 '22 at 14:33
  • This seems to be a known problem, and creating a custom form appears to be a better way to go. See https://stackoverflow.com/a/61569075/250962 – Phil Gyford Feb 10 '22 at 15:02
1

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.

Dan Swain
  • 2,910
  • 1
  • 16
  • 36
  • This could at least be a way to add help text to the `search_description` (which doesn't have any), but it doesn't really address the label changes. It seemed like the `heading` and `help_text` keyword args should be overriding those values in the `FieldPanel`, but maybe I've misunderstood how those are used. – darth_mall Aug 13 '19 at 17:36
  • Thanks, Dan, but that still doesn't really answer my question. I know you can't override the fields in the `Page` model, that's why I was asking about overriding labels and help text on the `FieldPanel`. It kinda looked like the `FieldPanel` constructor would allow you to specify those values using the `heading` and `help_text` keyword arguments, but that isn't working, so either that's not what those are for, I'm doing something wrong, or there's a bug. – darth_mall Aug 14 '19 at 15:20
0

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'
darth_mall
  • 984
  • 8
  • 16