Opened 18 years ago

Closed 17 years ago

#2482 closed enhancement (fixed)

[patch] Add optional flat values() to QuerySet

Reported by: mccutchen@… Owned by: Daniel Poelzleithner
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: no UI/UX: no

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@… 18 years ago.
Diff against trunk revision 3522
valuelist.diff (4.0 KB ) - added by mccutchen@… 18 years ago.
Patch which implements valuelist() method

Download all attachments as: .zip

Change History (18)

by mccutchen@…, 18 years ago

Attachment: values-flat.diff added

Diff against trunk revision 3522

comment:1 by anonymous, 18 years ago

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 by mccutchen@…, 18 years ago

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 by jim@…, 18 years ago

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 by anonymous, 18 years ago

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 by Adrian Holovaty, 18 years ago

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

comment:6 by mccutchen@…, 18 years ago

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?

by mccutchen@…, 18 years ago

Attachment: valuelist.diff added

Patch which implements valuelist() method

comment:7 by Malcolm Tredinnick, 18 years ago

Owner: changed from Adrian Holovaty to Malcolm Tredinnick

I'll merge this into the ongoing query rewrite.

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

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 by Chris Beaven, 18 years ago

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 by Michael Radziej <mir@…>, 18 years ago

Triage Stage: UnreviewedDesign decision needed

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

comment:11 by Daniel Poelzleithner, 17 years ago

Keywords: sprintdec01 added
Owner: changed from nobody to Daniel Poelzleithner
Patch needs improvement: set

comment:12 by Jacob, 17 years ago

Triage Stage: Design decision neededAccepted

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 by Chris Beaven, 17 years ago

Keywords: qs-rf added

comment:14 by Malcolm Tredinnick, 17 years ago

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

comment:15 by Malcolm Tredinnick, 17 years ago

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

comment:16 by Malcolm Tredinnick, 17 years ago

Resolution: fixed
Status: newclosed

(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

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