Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying limit and offset in Django QuerySet wont work

I'm using Django 1.6.5 and have MySQL's general-query-log on, so I can see the sql hitting MySQL.
And I noticed that Specifying a bigger limit in Django's QuerySet would not work:

>>> from blog.models import Author  
>>> len(Author.objects.filter(pk__gt=0)[0:999])
>>> len(Author.objects.all()[0:999])

And MySQL's general log showed that both query had LIMIT 21.

But a limit smaller than 21 would work, e.g. len(Author.objects.all()[0:10]) would make a sql with LIMIT 10.

Why is that? Is there something I need to configure?

like image 441
adamsmith Avatar asked Jun 04 '14 15:06

adamsmith


People also ask

What is offset in Django?

For pagination in Django models, you need to use limit offset. Suppose you have 20 rows in a queryset and you want to access the data in batches of 5. The syntax is [OFFSET:OFFSET+LIMIT] where offset would be the no. of rows which we want to skip and the limit is the total no.

Is Django QuerySet lazy?

This is because a Django QuerySet is a lazy object. It contains all of the information it needs to populate itself from the database, but will not actually do so until the information is needed.

How does QuerySet work in Django?

A QuerySet is a collection of data from a database. A QuerySet is built up as a list of objects. QuerySets makes it easier to get the data you actually need, by allowing you to filter and order the data.


1 Answers

It happens when you make queries from the shell - the LIMIT clause is added to stop your terminal filling up with thousands of records when debugging:

You were printing (or, at least, trying to print) the repr() of the queryset. To avoid people accidentally trying to retrieve and print a million results, we (well, I) changed that to only retrieve and print the first 20 results and print "remainder truncated" if there were more. This is achieved by limiting the query to 21 results (if there are 21 results there are more than 20, so we print the "truncated" message). That only happens in the repr() -- i.e. it's only for diagnostic printing. No normal user code has this limit included automatically, so you happily create a queryset that iterates over a million results.

(Source)

like image 147
akxlr Avatar answered Sep 19 '22 14:09

akxlr