On Mon, Jun 11, 2007 at 06:18:18PM +1000, Malcolm Tredinnick wrote:
> On Sun, 2007-06-10 at 15:45 -0400, Forest Bond wrote:
> > Using the sqlite3 backend, any slice operation on a QuerySet for which the 
> > slice
> > does not start at 0, a list is returned instead of a QuerySet, and the 
> > slice is
> > not performed in the database, but in Python.
> > 
> > Is this expected behavior? The sqlite3 docs indicate that offsets can be 
> > given,
> > so I see no reason this shouldn't be performed at the DB level.
> 
> I'm unable to repeat this behaviour. Whenever I construct a QuerySet and
> slice it, the LIMIT/OFFSET bits get added. The only time the slicing is
> done in Python is where I have already accessed the results of the
> QuerySet (so it has been cached). In those cases, Django knows not to do
> another round-trip to the database.

Ok, I understand the motivation for this design decision, but shouldn't the
slice still return a QuerySet?  I see no reason why the (internal) cache status
of the QuerySet should have such critical impact on the (external) API -- type
of returned objects.

I'm not sure if this is a bug or intended behavior:

--------------------------------------------------------------------------------
In [1]: from loadmonitor.models import *

In [2]: q = Load.objects.all()

In [3]: for load in q:
   ...:     pass
   ...:

In [4]: loads = q[5:10]

In [5]: type(loads)
Out[5]: <type 'list'>
--------------------------------------------------------------------------------

As you suspected, I have to force the whole set to be pulled in order to trigger
the issue by activating the internal cache.  My issue, of course, is that I
sometimes use this sort of pattern:

q = Model.objects.all()
if x:
    q = q.filter(constraint = value)
if y:
    q = q.filter(constraint2 = value2)
if z:
    q = q.order_by('a', 'b')
q = q[0:20]
# Now, is q a list or QuerySet? depends on how many rows are in the table ...
# So, this might cause an exception:
print q.values

Thoughts?  Is it a bug?  I guess the odds of actually wanting to do much more
after slicing are relatively low, but I _did_ actually run into this in a
real-life situation.

-Forest

Attachment: signature.asc
Description: Digital signature

Reply via email to