tombreit
tombreit

Reputation: 1379

Append content of field to title field in Wagtail-CMS-admin

In order to differentiate pages in Wagtail CMS admin (in a page listing view, not in the edit page view), the title is - in my case - not enough. I have a long list of pages of one page-type (say class BlogPage), and some of these pages could have the same title. So I would like to add a second identifying field (here: date_from, a DateField) to this title.

I thought of

class BlogPage(Page):
    ...
    def title(self):
    if self.date_from:
        return self.date_from + " - " + self.title
    else:
        return self.title

but this does not work, the page.title without my def is used for the corresponding Wagtail-admin-template.

Short version: How to pre/-append an existing field to the title in Wagtail-admin?

Upvotes: 1

Views: 1447

Answers (3)

Jordan Caras
Jordan Caras

Reputation: 93

If you'd like to replace the title across all of the model admin (Yes, this includes the Edit page), Wagtail has a built in mechanism for that.

def get_admin_display_title(self):
    return '{} - {}'.format(self.date_from, super().get_admin_display_title())

Upvotes: 1

tombreit
tombreit

Reputation: 1379

wagtailmodeladmin seems to do exactly what I've searched for: extending the wagtailadmin to display a defined set of fields - not only title - on a page model basis; like I would have it in the Django admin site.

This way I do not alter the default wagtailadmin page listing - like I've tried it in my question, but hook in an additional page listing as an extra wagtail-sidebar-navigation-entry.

Thanks to an other answer I stumbled upon this possiblity.

Upvotes: 0

Brett
Brett

Reputation: 4391

title is an actual Django model attribute, don't try to override it: https://docs.djangoproject.com/en/dev/topics/db/models/#field-name-hiding-is-not-permitted

The actual admin template from wagtailadmin/pages/list.html generates the list of pages in a loop using {% for page in pages %} and calls {{ page.title}} via an include (templates/wagtailadmin/pages/listing/_page_title_explore.html) extensively throughout. So based on inspecting the code, there is no support for this in Wagtail itself. Check the other includes templates/wagtailadmin/pages/listing/*.

However, Django supports overriding one app's templates with your own. You can copy this template into your project's template folder, keeping the same path (e.g., templates/wagtailadmin/pages/listing/_page_title_explore.html assuming your project is set up with a templates directory).

You'll have to replace the calls to page.title with your own version. A filter or tag might make this easier. Add the following filter to your templatetags (e.g. myapp/templatetetags/myapp_tags.py):

register = template.Library()

@register.filter
def uniquify_title(page):
    specific_page = page.specific
    try:
        return specific_page.date_from.strftime("%Y-%m-%d") + " - " + specific_page.title
    except AttributeError:
        return specific_page.title

And then replace usages of {{ page.title}} in the template starting around line 7 and 9 with:

{{ page|uniquify_title }}

The downside of this is you have to update your own version of list.html every time Wagtail is updated. You could try submitting an issue on Github and proposing a fork which supplies an "admin_title" callable or something like that.

Upvotes: 0

Related Questions