Django query question
I have a view & template which displays a list of types of items. This is ok, but instead of displaying all different the types of items for that client. What I want to do is display the list of types individually.
For example, if an Item has a type stored say "General". I want to display all items that has a type only called "General".
Views
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}开发者_如何学Go, context_instance = RequestContext(request))
Template
Summary for {{client.name}}
Total Number of Items: {{total_items}}
{%for item in items}
{{item.type}}
{%endfor%}
Sounds like you want to regroup on type.
You can try `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
精彩评论