如何在Django模板中使用select_related?

时间:2011-07-08 09:26:54

标签: django

class FinalVideo(models.Model):
    source = models.ForeignKey(UploadedVideo)
    filename = models.CharField(max_length=100)
class UploadedVideo(models.Model):
    type = models.CharField(max_length=10)
在urls.py中

'django.views.generic.list_detail.object_list', {'queryset': FinalVideo.objects.select_related()}, 
模板中的

{% for object in object_list %}

如何为每个对象输入模板?

1 个答案:

答案 0 :(得分:2)

您想通过FinalVideo的对象实例访问UploadedVideo类的字段“类型”吗? 试试这个: 在urls.py中:

'django.views.generic.list_detail.object_list', {'queryset': FinalVideo.objects.select_related("source").all()},

模板中的

{% for object in object_list %}
    {% for uploaded_video in object.source_set.all %}
        {{ uploaded_video.type }}
    {% endfor %}
{% endfor %}