Django查询问题

时间:2011-03-28 12:34:58

标签: python html django models

我有一个观点&模板,显示项目类型列表。这没关系,但不是为该客户端显示所有不同类型的项目。我想要做的是单独显示类型列表

例如,如果一个Item的类型存储了“General”。我想显示所有类型仅为“常规”的项目。

浏览

   def client_summary(request, client_id):
        client = None
        items = None
    try:
        client = models.Client.objects.get(pk = client_id)
        items = client.storageitem_set.all()
        total_items = items.count()
        except:
            return HttpResponse(reverse(return_clients))
        return render_to_response('client_summary.html', {'items':items, 'total_items':total_items, 'client':client}, context_instance = RequestContext(request))

模板

Summary for {{client.name}}
Total Number of Items: {{total_items}}
{%for item in items}
        {{item.type}}
{%endfor%}

2 个答案:

答案 0 :(得分:8)

听起来你想要regroup类型。

答案 1 :(得分:1)

您可以尝试`client.storageitem_set.filter(type = YOUR_TYPE):

def client_summary(request, client_id):
    client = None
    items = None
    try:
        client = models.Client.objects.get(pk = client_id)
        items = client.storageitem_set.filter(type="General")
        total_items = items.count()
        except:
           return HttpResponse(reverse(return_clients))
        return render_to_response('client_summary.html', {'items':items, 'total_items':total_items, 'client':client}, context_instance = RequestContext(request))

docs:Following relationships backwards