ForeignKey,在模板中显示值

时间:2011-02-05 22:22:53

标签: django django-templates

我有两个班级:

class People
    name = CharField()

class Equipment
    name = Charfield()
    responsible = ForeignKey(People)

并查看:

def persone_detail(request, tab_number):
    return direct_to_template(request, 'person.html', {
       'persone': Peoples.objects.filter(tab_number=tab_number)

如何在模板中显示设备中的名称

1 个答案:

答案 0 :(得分:5)

遵循与设备的反向关系: http://docs.djangoproject.com/en/dev/topics/db/queries/#following-relationships-backward

{% for person in persone %}
    Person: {{ person.name }}
    {% for equipment in person.equipment_set.all %}
        Equipment: {{ equipment.name }}
    {% endfor %}
{% endfor %}