Opened 17 years ago
Closed 15 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)
Changed 17 years ago by
Attachment: | values-flat.diff added |
---|
comment:1 Changed 17 years ago by
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 17 years ago by
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 17 years ago by
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 17 years ago by
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 17 years ago by
Thanks for the patch! Could you rewrite it (and the unit tests) to be a separate method called .valuelist
?
comment:6 Changed 17 years ago by
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:7 Changed 17 years ago by
Owner: | changed from Adrian Holovaty to Malcolm Tredinnick |
---|
I'll merge this into the ongoing query rewrite.
comment:8 Changed 17 years ago by
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 17 years ago by
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 16 years ago by
Triage Stage: | Unreviewed → Design decision needed |
---|
This needs a decision on how to treat single-field lists, see thread in comment above.
comment:11 Changed 16 years ago by
Keywords: | sprintdec01 added |
---|---|
Owner: | changed from nobody to Daniel Poelzleithner |
Patch needs improvement: | set |
comment:12 Changed 15 years ago by
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 Changed 15 years ago by
Keywords: | qs-rf added |
---|
comment:14 Changed 15 years ago by
comment:15 Changed 15 years ago by
Keywords: | qs-rf-fixed added; qs-rf removed |
---|
comment:16 Changed 15 years ago by
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