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 directly as a json (web /
> rpc usage), and I wonder if I must go from the cycle .
>
>
> class A(Base):
>     attr_one = Column(..)
>     attr_two = Column(..)
>     attr_three = Column(..)
>     attr_four = Column(..)
>     attr_five = Column(..)
>
> data = session.query(A.attr_one, A.attr_four)  # ok, got a query object
> result = data.all()  # got a result proxy
> wanted_result = map(lambda r: r._as_dict(), data.all())  # iterate through
> the result proxy, calling the _as_dict() method from row_proxy. it does the
> job, but is that really necessary?
>
>
> my question is if there's any way of "simplifying" the query just for json
> purposes, since imho the need of some proxies here may be an overkill. but
> ... i might be wrong :)
>

I'm not aware of any way of getting SQLAlchemy to return "plain old
dicts". How are you converting your Python dicts to JSON? JSON
converters often have a hook point where you can define how custom
objects are converted to JSON. Here's how you could do it in Pyramid:

http://pyramid.readthedocs.org/en/latest/narr/renderers.html#using-the-add-adapter-method-of-a-custom-json-renderer

And the Python json.dump function accepts a "default" parameter that
could do custom serialization.

Hope that helps,

Simon

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to