方式二:用模板语言
- get_server_status_id_display( ); #局限性:条件必须是对象QuerySet[对象,对象]
代码:
def test(request): """ 模板语言显示 choice :param request: :return: """ server_list = models.Server.objects.all() for row in server_list: print(row.id,row.hostname, "===", row.server_status_id,row.get_server_status_id_display() ) return render(request, 'test.html', {'server_list': server_list})
<ul> {% for row in server_list %} <li>{{ row.hostname }}---{{ row.get_server_status_id_display }}</li> {% endfor %} </ul>
-yield实现
代码:
def xx(data_list): for row in data_list: for item in models.Server.server_status_choices: if item[0] == row['server_status_id']: row['server_status_id_name'] = item[1] break print('***',item) yield row def test(request): """ 模板语言显示 choice :param request: :return: """ data_list =models.Server.objects.all().values('hostname','server_status_id') print(data_list) return render(request,'test.html',{'server_list':xx(data_list)}) #注意xx()
<ul> {% for row in server_list %} <li>{{ row.hostname }}---{{ row.server_status_id_name }}</li> {% endfor %} </ul>