Django

Code

Ticket #2482 (closed: fixed)

Opened 2 years ago

Last modified 3 weeks ago

[patch] Add optional flat values() to QuerySet

Reported by: mccutchen@gmail.com Assigned to: poelzi
Component: Database wrapper Version:
Keywords: sprintdec01 qs-rf-fixed Cc: mccutchen@gmail.com, gary.wilson@gmail.com
Triage Stage: Accepted Has patch: 1
Needs documentation: 0 Needs tests: 0
Patch needs improvement: 1

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

values-flat.diff (3.1 kB) - added by mccutchen@gmail.com on 08/04/06 11:56:49.
Diff against trunk revision 3522
valuelist.diff (4.0 kB) - added by mccutchen@gmail.com on 08/11/06 11:03:05.
Patch which implements valuelist() method

Change History

08/04/06 11:56:49 changed by mccutchen@gmail.com

  • attachment values-flat.diff added.

Diff against trunk revision 3522

08/04/06 11:58:02 changed 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.

08/04/06 12:06:46 changed by mccutchen@gmail.com

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.

08/04/06 18:50:46 changed by jim@dsdd.org

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.

08/07/06 09:31:37 changed 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.

08/11/06 00:10:00 changed by adrian

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

08/11/06 11:02:32 changed by mccutchen@gmail.com

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?

08/11/06 11:03:05 changed by mccutchen@gmail.com

  • attachment valuelist.diff added.

Patch which implements valuelist() method

09/23/06 03:43:24 changed by mtredinnick

  • owner changed from adrian to mtredinnick.

I'll merge this into the ongoing query rewrite.

10/17/06 10:38:08 changed by Gary Wilson <gary.wilson@gmail.com>

  • cc changed from mccutchen@gmail.com to mccutchen@gmail.com, gary.wilson@gmail.com.

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 :(

11/02/06 17:13:07 changed 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.

02/03/07 05:53:40 changed by Michael Radziej <mir@noris.de>

  • stage changed from Unreviewed to Design decision needed.

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

12/01/07 13:09:41 changed by poelzi

  • keywords set to sprintdec01.
  • needs_better_patch set to 1.
  • owner changed from nobody to poelzi.

12/02/07 12:41:07 changed by jacob

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

02/21/08 15:18:45 changed by SmileyChris

  • keywords changed from sprintdec01 to sprintdec01 qs-rf.

02/22/08 21:36:39 changed by mtredinnick

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

02/22/08 21:37:13 changed by mtredinnick

  • keywords changed from sprintdec01 qs-rf to sprintdec01 qs-rf-fixed.

04/26/08 21:50:16 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(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/Change #2482 ([patch] Add optional flat values() to QuerySet)




Change Properties
Action