我需要在列表中显示Django Admin中的图标,并根据值更改视图。
例如:
如果模型域eventId
有值1
,则显示图标(png或webfont)
这可能吗?
模型:
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'))
创建一个类似icon()的方法并将其添加到list_display, fields并将其设置为只读。
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