Currently it's kinda verbose to get just a list of id's from a query to
put it in a bulk_in or process it further.
example:
>>> NetworkState.objects.all().order_by("-stopdate")[:5].values("id")
[{'id': 2738}, {'id': 2737}, {'id': 2736}, {'id': 2735}, {'id': 2734}]
Then i have to unpack it to get a [2738, 2737,...] list, which is what I
wanted in the first place.
I suggest Object.objects.all().values("id", flat=True). Flat as a
optional argument, which is only allowed when only one field is
requested and returns a list with only the values.
Any better ideas ?
kindly regards
Daniel
Oooh, that's a nice idea. I'm not 100% certain "flat=True" is the best
name for it, but I can't think of anything better at the moment. Would
you be willing to submit a patch?
Adrian
--
Adrian Holovaty
holovaty.com | djangoproject.com
NetworkState.objects.all().order_by("-stopdate")[:5].values("id",
"name")
I couldn't think of a nice solution at first, so i said for one key only.
What do you think about returning n tupples for n arguments. if n > 1
and a pure list for only one argument ?
.values("id", "name", flat=True) would return
[(id,name),(id,name),...]
.values("id", flat=True) would return
[id,id,...]
?
kindly regards
PS: yes i will provide a patch :)
kindly regards
Daniel
Sounds pretty good to me. Let's do it.
Maybe .flatvalues("id", "name") or .valuelist("id", "name") ?
I opened a ticket and submitted a patch:
http://code.djangoproject.com/ticket/2482
My patch adds a keyword arguement to the 'values' method, as originally
proposed. It could be adapted pretty easily to Gary's suggestion of a
separate 'flatvalues' or 'valuelist' method.
Will.