Opened 5 years ago

Closed 5 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


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)
> rlist.count(1)
  TypeError: count() takes exactly 1 argument (2 given)
> rlist2 = list(Rpt.objects.values_list('status', flat=True))
> rlist2.count(1)

Attachments (2)

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

Download all attachments as: .zip

Change History (10)

comment:1 Changed 5 years ago by Baptiste Mispelon

Triage Stage: UnreviewedAccepted

Sounds like a reasonable addition.

comment:2 Changed 5 years ago by Daniele Procida

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 Changed 5 years ago by alextreme

Owner: changed from Daniele Procida to alextreme

Changed 5 years ago by alextreme

comment:4 Changed 5 years ago by alextreme

Has patch: set

Added a patch to clarify the values_list() documentation.

comment:5 Changed 5 years ago by Baptiste Mispelon

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).

Changed 5 years ago by alextreme

comment:6 Changed 5 years ago by alextreme

Thanks, the ticket has been updated.

comment:7 Changed 5 years ago by Baptiste Mispelon

Triage Stage: AcceptedReady for checkin

Looks good. Thanks for the patch!

comment:8 Changed 5 years ago by Aymeric Augustin <aymeric.augustin@…>

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