3

我有两个定义两个模型的类

class Master(models.Model):
    date = models.DateTimeField()
    status = models.CharField(default = 'R')

class Detail(models.Model):
    name = models.TextField()
    from = models.ForeignKey(Master)

风景:

def list_view(request):
    masters = master.objects.filter()
    context = {'masters': masters}
    return render_to_response('list.html', context, context_instance = RequestContext(request))

模板:

{% for master in masters %}
    <tr>
        <td>{{ master.date }}</td>
        <td>{{ master.status }}</td>
        <td>{# THIS #}</td>
    </tr>
{% endfor %}

我想在 {# THIS #} 部分显示详细名称,但我不知道如何为此修改视图。如何访问模板中主控的详细信息?

4

1 回答 1

3

你可以做这样的事情来获得names

{% for master in masters %}
    <tr>
        <td>{{ master.date }}</td>
        <td>{{ master.status }}</td>
        <td>
           {% for detail in master.detail_set.all %}
               {{detail.name}}<br/>
           {% endfor %}        
        </td>
    </tr>
{% endfor %}
于 2013-05-03T14:47:59.873 回答