Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify max results for a filter query in Django?

I want to retrieve the first 500 results from a large database that match a given filter query.

At the moment I'm using the following (terribly inefficient) method..

results = Entries.objects.filter(text__icontains="somequery")[0:500]

But I think this query loads the entire database in memory and then truncates the results. It's terribly slow.

Is there a more elegant way to do this? Thanks!

like image 351
infiniteloop Avatar asked Feb 19 '11 21:02

infiniteloop


People also ask

How do I limit query results in Django?

Use a subset of Python's array-slicing syntax to limit your QuerySet to a certain number of results. This is the equivalent of SQL's LIMIT and OFFSET clauses. Negative indexing (i.e. Entry.objects.all()[-1] ) is not supported.

What does filter return in Django?

The filter() method is used to filter you search, and allows you to return only the rows that matches the search term.

What is the difference between filter and get method in Django?

Returns a new QuerySet containing objects that match the given lookup parameters. Basically use get() when you want to get a single unique object, and filter() when you want to get all objects that match your lookup parameters.


1 Answers

That's the way to do it.

The SQL generated uses LIMIT so it's not loading the entire database into memory and being python sliced.

Note that you can see what SQL django is writing by using django.db.connection.queries http://docs.djangoproject.com/en/dev/faq/models/#how-can-i-see-the-raw-sql-queries-django-is-running

But a lesser known trick is to print a queryset.query or call sql = queryset.query.__str__()

>>> results = Entries.objects.filter(text__icontains="somequery")[0:500]
>>> print results.query
SELECT ... FROM ... WHERE ... LIKE ... LIMIT 500
like image 124
Yuji 'Tomita' Tomita Avatar answered Oct 21 '22 10:10

Yuji 'Tomita' Tomita