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