Code

Opened 8 years ago

Closed 6 years ago

#2482 closed enhancement (fixed)

[patch] Add optional flat values() to QuerySet

Reported by: mccutchen@… Owned by: poelzi
Component: Database layer (models, ORM) Version:
Severity: normal Keywords: sprintdec01 qs-rf-fixed
Cc: mccutchen@…, gary.wilson@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: UI/UX:

Description

The attached patch adds a keyword argument, 'flat', to the QuerySet.values() method, which causes it to return a flattened list of values rather than a list of dictionaries.

See discussion here:
http://groups.google.com/group/django-developers/browse_thread/thread/0b1897d4d5dd7277/1ed627b92f3e56c9#1ed627b92f3e56c9

Attachments (2)

values-flat.diff (3.1 KB) - added by mccutchen@… 8 years ago.
Diff against trunk revision 3522
valuelist.diff (4.0 KB) - added by mccutchen@… 8 years ago.
Patch which implements valuelist() method

Download all attachments as: .zip

Change History (18)

Changed 8 years ago by mccutchen@…

Diff against trunk revision 3522

comment:1 Changed 8 years ago by anonymous

This is my first attempt to contribute code to Django. I added a couple of tests to the lookup tests, I hope they are appropriate.

comment:2 Changed 8 years ago by mccutchen@…

An alternative approach, as suggested by Gary Wilson in the thread linked above, is to add a separate method to QuerySet objects:

Maybe .flatvalues("id", "name") or .valuelist("id", "name")

If that is the preferred route, I would vote for 'valuelist' as the name of the method.

comment:3 Changed 8 years ago by jim@…

I'd like to suggest .items("id", "name"). With no parameters, it would would default to all fields in creation order.

This maps up nicely with dictionary object's "items" method.

comment:4 Changed 8 years ago by anonymous

I'd like to suggest .items("id", "name"). With no parameters, it would would default to all fields in creation order.

This maps up nicely with dictionary object's "items" method.

I'm not so sure about this. The dictionary object's "items" method returns tuples of (key, value) in a dictionary, whereas the whole point of this flag/new method is to remove the keys altogether, when you just want some values. So naming the same as the dictionary method would be misleading.

comment:5 Changed 8 years ago by adrian

Thanks for the patch! Could you rewrite it (and the unit tests) to be a separate method called .valuelist?

comment:6 Changed 8 years ago by mccutchen@…

A new patch is attached, valuelist.diff, which creates a new method called valuelist(). This patch adds a new class, ValueListQuerySet, which inherits from ValuesQuerySet. I factored some of the logic out of ValuesQueryset's iterator() method into a get_columns() method and a get_field_names() method, so that the same logic could be used in the ValueListQuerySet object.

There are two doctests added, and they both pass.

I hope this is an appropriate solution.

P.S. The last diff I attached could not be previewed by Trac for some reason. I'm about to attach a new one. If it doesn't work either, does anyone have any idea what I'm doing wrong?

Changed 8 years ago by mccutchen@…

Patch which implements valuelist() method

comment:7 Changed 8 years ago by mtredinnick

  • Owner changed from adrian to mtredinnick

I'll merge this into the ongoing query rewrite.

comment:8 Changed 8 years ago by Gary Wilson <gary.wilson@…>

  • Cc gary.wilson@… added

Thanks for the patch Will. I finally needed to get a list of ids the other day and was remembering about this. Sadly, it's not yet merged :(

comment:9 Changed 7 years ago by SmileyChris

A good point is made here: http://groups-beta.google.com/group/django-developers/browse_thread/thread/22b44f4eafaf956a/1515cf99737ab589#1515cf99737ab589

I'd also prefer a way to ensure tuples are always returned.

comment:10 Changed 7 years ago by Michael Radziej <mir@…>

  • Triage Stage changed from Unreviewed to Design decision needed

This needs a decision on how to treat single-field lists, see thread in comment above.

comment:11 Changed 6 years ago by poelzi

  • Keywords sprintdec01 added
  • Owner changed from nobody to poelzi
  • Patch needs improvement set

comment:12 Changed 6 years ago by jacob

  • Triage Stage changed from Design decision needed to Accepted

I'm not a huge fan of valuelist() since it smells a bit of creeping featurism, but since Adrian likes this I'll go ahead and mark it accepted.

comment:13 Changed 6 years ago by SmileyChris

  • Keywords qs-rf added

comment:14 Changed 6 years ago by mtredinnick

(In [7149]) queryset-refactor: Added valuelist() method to querysets. Refs #2482.

comment:15 Changed 6 years ago by mtredinnick

  • Keywords qs-rf-fixed added; qs-rf removed

comment:16 Changed 6 years ago by mtredinnick

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

(In [7477]) Merged the queryset-refactor branch into trunk.

This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.

Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.