1

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}, context_instance = RequestContext(request))

Template

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

2 Answers 2

8

Sounds like you want to regroup on type.

Sign up to request clarification or add additional context in comments.

Comments

1

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

3 Comments

that will only allow for one type though? I think he wants to list all the types that may exist
yes, but he can use an __in query, the idea is to read the docs :)
Actually this is what I want. This does work. The problem is that each type has a number. So it would complain that the value is not an interger.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.