Opened 2 years ago

Closed 2 years ago

#20408 closed Cleanup/optimization (fixed)

values_list(flat=True) returns ValuesListQuerySet, not list

Reported by: marktranchant 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 2 years ago.
django_docs_ref_models_queryset_2.diff (677 bytes) - added by alextreme 2 years ago.

Download all attachments as: .zip

Change History (10)

comment:1 Changed 2 years ago by bmispelon

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

Sounds like a reasonable addition.

comment:2 Changed 2 years ago by EvilDMP

  • Owner changed from nobody to EvilDMP
  • Status changed from new to 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 Changed 2 years ago by alextreme

  • Owner changed from EvilDMP to alextreme

Changed 2 years ago by alextreme

comment:4 Changed 2 years ago by alextreme

  • Has patch set

Added a patch to clarify the values_list() documentation.

comment:5 Changed 2 years ago by bmispelon

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

comment:6 Changed 2 years ago by alextreme

Thanks, the ticket has been updated.

comment:7 Changed 2 years ago by bmispelon

  • Triage Stage changed from Accepted to Ready for checkin

Looks good. Thanks for the patch!

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

  • Resolution set to fixed
  • Status changed from assigned to closed

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