I need to show an Icon in Django Admin in the list and change view based on the value.
For example:
If Model Field eventId
has value 1
then show icon (png or webfont)
Is this possible?
Model:
class Events(models.Model):
eventId = models.CharField(_('EventID'), max_length=254, null=False, blank=False, default='', )
name = models.CharField(_('EventName'), max_length=254, null=False, blank=False, default='', )
euuid = models.UUIDField(_('UUID'), default=uuid.uuid4, editable=False, unique=True)
created = models.DateTimeField(_('Created'), auto_now_add=True, blank=True, help_text=_('Date of the creation'))
CodePudding user response:
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