基于这个答案https://stackoverflow.com/a/14498938 .in Django 模型我添加
class Meta:
app_name = 'myapp'
在列表视图中我有
from django.core import serializers
context['data'] = serializers.serialize( "python", self.get_queryset() )
在 mylist.html 我有
{% for field, value in data.0.fields.items %}
<th style="text-align:center;">{% get_verbose_field_name data.0.model field %}</th>
{% endfor %}
在过滤器中:
from django import template
register = template.Library()
from .models import Mymodel
@register.simple_tag
def get_verbose_field_name(instance, field_name):
"""
Returns verbose_name for a field.
"""
myinstance = eval(instance.split('.')[1].title())
return myinstance._meta.get_field(field_name).verbose_name.title()
具体示例的上述过滤器中的实例是 myapp.mymodel 我将实例评估为模型对象,并且我返回字段详细名称
它适用于 django 1.9