[sqlalchemy] how to return an array of dicts

2015-06-01 Thread Richard Gerd Kuesters
hello all! probably this was asked before, as I already grabbed some answers already from here and stackoverflow, but I don't really feel happy about it. problem: i have a query that it's result must go directly as a json (web / rpc usage), and I wonder if I must go from the cycle . class

Re: [sqlalchemy] how to return an array of dicts

2015-06-01 Thread Richard Gerd Kuesters
thanks Simon! yes, i'm already using hooks so I can pass datetime, decimal, enums and so on; of course, it can help if I have to go with the result proxy. i just wonder if there's another way of doing this without having sqlalchemy to provide me helpers of proxy objects. i'm thinking about

Re: [sqlalchemy] how to return an array of dicts

2015-06-01 Thread Simon King
On Mon, Jun 1, 2015 at 1:51 PM, Richard Gerd Kuesters rich...@pollux.com.br wrote: hello all! probably this was asked before, as I already grabbed some answers already from here and stackoverflow, but I don't really feel happy about it. problem: i have a query that it's result must go

Re: [sqlalchemy] how to return an array of dicts

2015-06-01 Thread Richard Gerd Kuesters
Thanks Jonathan, I agree with you, 100%. I have methods for that also, when I have to deal with the real objects and queries and stuff. The point, in my question, is that I have some services that are not vital to my application, but are used constantly -- and it just spits out data. I'm

Re: [sqlalchemy] how to return an array of dicts

2015-06-01 Thread Richard Gerd Kuesters
argh! results = map(lambda r: dict(r.items()), session.execute(my_select).fetchall()) much simplier :) but, the question persists: is this the best approach for a raw data dictionary result query? best regards, richard. On 06/01/2015 10:22 AM, Richard Gerd Kuesters wrote: well, i can

Re: [sqlalchemy] how to return an array of dicts

2015-06-01 Thread Jonathan Vanasco
All my models inherit from an additional base class with this method: def columns_as_dict(self): return a dict of the columns; does not handle relationships return dict((col.name, getattr(self, col.name)) for col in sqlalchemy_orm.class_mapper(self.__class__).mapped_table.c)

Re: [sqlalchemy] how to return an array of dicts

2015-06-01 Thread Richard Gerd Kuesters
well, i can use select and zip ... don't know if this is the best approach: foo = session.execute(my_select) # my_select have the same rules as the session.query(A..., A).filter(...).order_by(...).offset(...).limit() results = map(lambda r: dict(foo.keys(), r), foo.fetchall()) any