wagtail

Is there a way to show images in a Wagtail Model Admin Record Listing page?


I have reviewed the question on Is there any way to show a field on a listing page in Wagtail admin? but my situation seems to similar but also different enough that that particular solution won't work for me. Instead of on the Page listing I wish to achieve a similar thing on the Model Admin listing and I would think this should be such a common requirement that I am picking that someone must have done this before I have attempted it.

I haven't really figured out how to even try anything to get started but what I have looked at is the modeladmin template tags under wagtail.contrib.modeladmin on GitHub but I am completely guessing.

Can anyone point me to which templates I need to modify and whether I need to modify any of the template tags and how to override anything I need to override?


Solution

  • There's no need to override templates for this - this is standard functionality in ModelAdmin. Adding extra fields to the listing is done by setting list_display on the ModelAdmin class:

    class BookAdmin(ModelAdmin):
        model = Book
        list_display = ('title', 'author')
    

    For displaying images, ModelAdmin provides ThumbnailMixin:

    from wagtail.contrib.modeladmin.mixins import ThumbnailMixin
    from wagtail.contrib.modeladmin.options import ModelAdmin
    
    class BookAdmin(ThumbnailMixin, ModelAdmin):
        model = Book
        thumb_image_field_name = 'cover_image'
        list_display = ('title', 'author', 'admin_thumb')
    

    ('admin_thumb' is a special-purpose field name provided by ThumbnailMixin, and should be used rather than the actual image field on your model - cover_image in this example.)