Django小知识点整理

时间:2024-08-18 13:02:50

一、Django中Choices字段显示中文的两种方式

  1、get_xxxx_display():这种方式只能针对对象来使用

{% for row in server_list %}
<li>{{ row.hostname }}----{{ row.get_xxxx_display }}</li>
{% endfor %}

  2、yield实现:这种方式可以用于QuerySet

def xxxx(server_list):
for row in server_list:
for item in models.Server.server_status_choices:
if item[0] == row['server_status_id']:
row['server_status_id_name'] = item[1]
break
yield row def test(requests):
data_list = models.Server.objects.all().values('hostname','server_status_id')
return render(request,'test.html',{"server_list"}:xxxx(data_list))