List_display. How to display value from options?

STATUS = ( (1, "Sent"), (2, "Not send",),) class Log(models.Model): status = models.CharField(max_length=255,choices=STATUS) 

admin

 class LogAdmin(admin.ModelAdmin): list_display=['status'] 

This display:

Status - (leer)

How to display status - sent or not sent here?

+6
source share
2 answers

just use:

 class LogAdmin(admin.ModelAdmin): list_display=['get_status_display'] 

Django documentation: get_FOO_display

+14
source

(Updated to django 1.8) it is best to create a function

 def get_status(self, obj): return obj.get_status_display() get_status.short_description = 'Status' 

Put 'get_status' in list_display and Status, this will be the column name in the admin list.

+5
source

Source: https://habr.com/ru/post/943699/


All Articles