Opened 18 years ago
Closed 17 years ago
#2482 closed enhancement (fixed)
[patch] Add optional flat values() to QuerySet
Reported by: | 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)
Change History (18)
by , 18 years ago
Attachment: | values-flat.diff added |
---|
comment:1 by , 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 , 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 , 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 , 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 , 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 , 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?
comment:8 by , 18 years ago
Cc: | 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 , 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 , 18 years ago
Triage Stage: | Unreviewed → Design decision needed |
---|
This needs a decision on how to treat single-field lists, see thread in comment above.
comment:11 by , 17 years ago
Keywords: | sprintdec01 added |
---|---|
Owner: | changed from | to
Patch needs improvement: | set |
comment:12 by , 17 years ago
Triage Stage: | Design decision needed → 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 by , 17 years ago
Keywords: | qs-rf added |
---|
comment:14 by , 17 years ago
comment:15 by , 17 years ago
Keywords: | qs-rf-fixed added; qs-rf removed |
---|
comment:16 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | new → 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
Diff against trunk revision 3522