Comments regarding QuerySet.valuelist()

92 views
Skip to first unread message

Michael Radziej

unread,
Oct 31, 2006, 2:49:19 PM10/31/06
to django-d...@googlegroups.com
Hi,

Ticket 2482 by mccutchen (thanks!) adds a method 'valuelist()' to
QuerySet. It works similar to values(), but generates tuples
instead of dicts.

I use this, and I really appreciate the idea, but there's one
thing I don't like about it. If you pass only one attribute, it
yields single values, not singletons, i.e.

>>> list(qset.valuelist("a","b"))
[(a1,b1), (a2,b2), ...]

>>> list(qset.valuelist("a"))
[a1, a2]

This is very neat most times, especially when you retrieve ids to
pass to bulk_in, but as soon as you start to use this more
generically like

valuelist(*attnames)

you get different structures depending on the length of attnames,
and this feels so wrong. It leads to code like this:

qset_hits = data_source.query_set().filter(
**{"%s__istartswith" % data_source.att_names[0]: query}
).valuelist(*data_source.att_names)
if len(self.att_names)==1:
qset_hits = ((x,) for x in qset_hits)

I propose to split the functionality into two separate methods,
valueslist() and valuetupels(). valueslist() would only allow to
pass in a single attribute name.

valuelist(attname) --> always yields single values
e.g., list(valuelist("a")) --> [a1, a2, ...]
valuetuples(*attnames) --> always yields tuples
e.g., list(valuetuples("a")) --> [(a1,), (a2,), ...]

What do you think? Am I already splitting hairs?

Michael


--
noris network AG - Deutschherrnstraße 15-19 - D-90429 Nürnberg -
Tel +49 911 9352-0 - Fax +49 911 9352-100

http://www.noris.de - The IT-Outsourcing Company

Gary Wilson

unread,
Nov 4, 2006, 5:32:58 PM11/4/06
to Django developers
I think you raise a valid point. There are valid use cases for both
methods.

James Bennett

unread,
Nov 4, 2006, 6:57:38 PM11/4/06
to django-d...@googlegroups.com
On 10/31/06, Michael Radziej <m...@noris.de> wrote:
> I propose to split the functionality into two separate methods,
> valueslist() and valuetupels(). valueslist() would only allow to
> pass in a single attribute name.
>
> valuelist(attname) --> always yields single values
> e.g., list(valuelist("a")) --> [a1, a2, ...]
> valuetuples(*attnames) --> always yields tuples
> e.g., list(valuetuples("a")) --> [(a1,), (a2,), ...]
>
> What do you think? Am I already splitting hairs?

No, I think you're right that we should have a mechanism in place for
ensuring that methods like this always return results of a consistent
type. I'm not sure whether it'd be better to do two methods or to
return singleton tuples, though; anybody got thoughts one way or
another?

--
"May the forces of evil become confused on the way to your house."
-- George Carlin

Gary Wilson

unread,
Nov 4, 2006, 11:34:42 PM11/4/06
to Django developers
> No, I think you're right that we should have a mechanism in place for
> ensuring that methods like this always return results of a consistent
> type. I'm not sure whether it'd be better to do two methods or to
> return singleton tuples, though; anybody got thoughts one way or
> another?

Well, a lot of times, I see myself needing just a simple list (list of
titles, list of id's to pass to in_bulk(), etc.) If we just had a
function that returned tuples, then a list comprehension would still
have to be used to make a list from the singleton tuples when wanting a
for loop or for passing to in_bulk().

With adding both methods, we then have a method for returning each of
the main python data structures. As for valuelist(), instead of being
able to only pass a single parameter, it might be cool to make a zip'ed
list of values, i.e.

>>> list(qset.valuelist("a","b"))

[a1, b1, a2, b2, ...]

Reply all
Reply to author
Forward
0 new messages