Django.fun

How to show in Django Admin Icons based on model value

I need to show an Icon in Django Admin based on the value.

For example:

If Model Field weather has value sun then show icon sun (png or webfont)

Is this possible?

Answers: 1

Answered by shahbaz ahmad, Oct. 22, 2021, 10:35 a.m.

Your create a method like icon() and add this to list_display, fields and make it as readonly.

from django.utils.html import format_html

class EventsAdmin(admin.ModelAdmin):
    ...
    ...

    list_display = ['your_list_display_fields', 'icon']

    fields = ['your_model_fields_for_form', 'icon']

    readonly_fields = ['icon']

    def icon(self, obj):
        if obj.eventId == 1:
            icon_url = '<your_icon_url>'     # get icon url 
            return format_html('<img src="{}">', icon_url)
        return None