That's already done by @expose('json') for you:

    @expose('json')
    def test(self):
        return dict(user=DBSession.query(model.User).first())

If you need the result of the encoding you can do:

        from tg import json_encode
        userjson = json_encode(DBSession.query(model.User).first())

tg.json_encode is just an alias for tg.jsonify.encode ->
http://turbogears.readthedocs.org/en/latest/reference/classes.html#tg.jsonify.encode
which by itself just creates an instance of JSONEncoder (
http://turbogears.readthedocs.org/en/latest/reference/classes.html#tg.jsonify.JSONEncoder
) which is able to encode both SQLAlchemy and Ming queries and provide
custom encoding for your types or models.

On Tue, Sep 15, 2015 at 10:20 PM, Paul Kraus <[email protected]> wrote:

> Thanks i guess i need to dig through the docs some more. Is there a way to
> easily turn a sqlalchemy model object into a dictionary/JSON?
>
> Currently i just add on a property as_dict that returns the attributes i
> need as a dictionary but thought i might ask just in case there was some
> utilities already baked in for this.
>
>
> On Tuesday, September 15, 2015 at 3:03:01 PM UTC-4, Alessandro Molina
> wrote:
>>
>> Hi Paul,
>>
>> That feature is now builtin in TG2 ad @decode_params, see
>> http://turbogears.readthedocs.org/en/latest/reference/classes.html?highlight=decode_params#tg.decorators.decode_params
>> :)
>>
>> On Tue, Sep 15, 2015 at 8:18 PM, Paul Kraus <[email protected]> wrote:
>>
>>> Putting this here
>>> http://code.runnable.com/U8WDEFXA3A47DT0X/parse-json-data-from-request-in-turbogears-for-python
>>>
>>> Found this gem and it made my current project a million times easier.
>>> Maybe reference this on the restfulapi doc page?
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "TurboGears" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> To post to this group, send email to [email protected].
>>> Visit this group at http://groups.google.com/group/turbogears.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "TurboGears" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/turbogears.
> For more options, visit https://groups.google.com/d/optout.
>

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

Reply via email to