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)

django_docs_ref_models_queryset.diff (927 bytes ) - added by alextreme 12 years ago.
django_docs_ref_models_queryset_2.diff (677 bytes ) - added by alextreme 12 years ago.

Download all attachments as: .zip

Change History (10)

comment:1 by Baptiste Mispelon, 12 years ago

Triage Stage: UnreviewedAccepted

Sounds like a reasonable addition.

comment:2 by Daniele Procida, 12 years ago

Owner: changed from nobody to Daniele Procida
Status: newassigned

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 alextreme, 12 years ago

Owner: changed from Daniele Procida to alextreme

by alextreme, 12 years ago

comment:4 by alextreme, 12 years ago

Has patch: set

Added a patch to clarify the values_list() documentation.

comment:5 by Baptiste Mispelon, 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).

comment:6 by alextreme, 12 years ago

Thanks, the ticket has been updated.

comment:7 by Baptiste Mispelon, 12 years ago

Triage Stage: AcceptedReady for checkin

Looks good. Thanks for the patch!

comment:8 by Aymeric Augustin <aymeric.augustin@…>, 12 years ago

Resolution: fixed
Status: assignedclosed

In 7b85ef9dfb83bd2f2cde46b9836b9fd12a033b26:

Fixed #20408 -- Clarified that values_list() doesn't return a list.

Thanks marktranchant, bmispelon, and alextreme.

Note: See TracTickets for help on using tickets.
Back to Top