Opened 12 years ago
Closed 12 years ago
#20408 closed Cleanup/optimization (fixed)
values_list(flat=True) returns ValuesListQuerySet, not list
Reported by: | Mark Tranchant | Owned by: | alextreme |
---|---|---|---|
Component: | Documentation | Version: | 1.4 |
Severity: | Normal | Keywords: | values_list queryset list |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
The documentation should be updated to clarify that the structure returned by a values_list queryset with flat=True is not a plain list, but a ValuesListQuerySet. Alternatively, the code should actually output a list.
I spent a while struggling to understand why I couldn't use the Python x.count(y) function on the output of such a query before realizing that I wasn't working with a Python list:
> rlist = Rpt.objects.values_list('status', flat=True) > type(rlist) django.db.models.query.ValuesListQuerySet > rlist.count(1) TypeError: count() takes exactly 1 argument (2 given) > rlist2 = list(Rpt.objects.values_list('status', flat=True)) > rlist2.count(1) 6
Attachments (2)
Change History (10)
comment:1 by , 12 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 12 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
I have tentatively reserved this ticket for first-time committers who take part in the Don't be afraid to commit workshop at the DjangoCon Europe 2013 sprints on 18th and 19th May.
If you want to tackle this ticket before then, please don't let the fact that it's assigned to me stop you. Feel free to re-assign it to yourself and do whatever you like to it.
comment:3 by , 12 years ago
Owner: | changed from | to
---|
by , 12 years ago
Attachment: | django_docs_ref_models_queryset.diff added |
---|
comment:4 by , 12 years ago
Has patch: | set |
---|
Added a patch to clarify the values_list() documentation.
comment:5 by , 12 years ago
How about something like that:
Note that this method returns a VLQS which is an object that behaves like
a list. Most of the time this is enough, but if you require an actual python
list object, you can simply call list() on it (which will evaluate the queryset).
by , 12 years ago
Attachment: | django_docs_ref_models_queryset_2.diff added |
---|
comment:7 by , 12 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Looks good. Thanks for the patch!
comment:8 by , 12 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
Sounds like a reasonable addition.