Opened 16 years ago
Closed 13 years ago
#10139 closed Bug (fixed)
Slicing an EmptyQuerySet gives a list, not another EmptyQuerySet
Reported by: | Forest Bond | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Ori Livneh | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
This causes problems if you want to do, for example, qs[0:1].get()
, which is, arguably, poor form. Still, I think this is a bug.
In [25]: type(models.TestRun.objects.none()[0:10]) Out[25]: <type 'list'> In [26]: type(models.TestRun.objects.all()[0:10]) Out[26]: <class 'django.db.models.query.QuerySet'>
Attachments (1)
Change History (10)
comment:1 by , 16 years ago
comment:2 by , 16 years ago
Triage Stage: | Unreviewed → Design decision needed |
---|
comment:3 by , 16 years ago
Component: | Uncategorized → Database layer (models, ORM) |
---|
comment:4 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → Bug |
by , 13 years ago
Attachment: | queryset_slice_return_type.diff added |
---|
document which type will be returned when slicing a QuerySet
comment:5 by , 13 years ago
Cc: | added |
---|---|
Easy pickings: | unset |
Has patch: | set |
UI/UX: | unset |
Version: | 1.0-beta-1 → SVN |
EmptyQuerySet
is a red herring -- the list is being returned by __getitem__
on the superclass, which is QuerySet
. The real problem is that the documentation for QuerySet
doesn't currently do a good job explaining what type object slicing a QuerySet returns. I've added a patch to improve the documentation on this topic.
comment:6 by , 13 years ago
Triage Stage: | Design decision needed → Ready for checkin |
---|
comment:7 by , 13 years ago
Don't forget to fix the double 'a' before the commit (Slicing a a QuerySet).
comment:8 by , 13 years ago
In fact there's no such thing as an unevaluated EmptyQuerySet
: __init__
sets self._result_cache = []
.
This isn't quite as clear as it looks. Slicing a normal
QuerySet
will sometimes return a list as well; it depends upon whether the results have already been retrieved from the database or not.Probably the solution here is a documentation one, but it's worth thinking about what else might be possible. Always returning a
QuerySet
, though, is, from memory, a little fiddly to organise efficiently and always correctly.