Shehzad009
Shehzad009

Reputation: 1607

Django: want to improve speed of query

I have a django app which displays the list of items for each client. it displays an item id, an alternative id, title and more other information. Because I have a lot of I items (Each client could have thousand of items), it can take very long to load up all of the items. Is there a way to improve this speed?

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

{% for item in items %}
        <tr class="items_table_row">
                <td><input type="checkbox" name="{{item.pk}}" value="{{item.pk}}" checked="checked"></td>
                <td>{{item.company_id}}</td><td>{{item.alternative_id}}</td><td>{{item.title}}</td><td>{{item.type}}</td><td>{{item.format}}</td>
                <td><span id="{{item.pk}}" name="type">{{item.itemstatushistory_set.latest}}</span></td><td>{{item.itemstatushistory_set.latest.date.date|date:"d M Y"}}</td>
{% endfor %}

Upvotes: 0

Views: 365

Answers (3)

Cesar Canassa
Cesar Canassa

Reputation: 20253

Make sure that the items are loaded with a constant number of database queries. For most views, you should be able to load all your data with only one database read.

If you notice that the queries are increasing or decreasing depending on how many items you display, you will probably need to include a select_related() in your queryset.

Upvotes: 0

vartec
vartec

Reputation: 134721

Paginate results into something more manageable than thousands of item. Without pagination it not only slow, but also gives bad user experience.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799470

"Pagination"

Upvotes: 2

Related Questions