TG / Genshi / Angular (NOT SPA)

23 views
Skip to first unread message

Paul Kraus

unread,
Sep 15, 2015, 2:18:05 PM9/15/15
to TurboGears
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?

Alessandro Molina

unread,
Sep 15, 2015, 3:03:01 PM9/15/15
to TurboGears

On Tue, Sep 15, 2015 at 8:18 PM, Paul Kraus <pkr...@pelsupply.com> 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 turbogears+...@googlegroups.com.
To post to this group, send email to turbo...@googlegroups.com.
Visit this group at http://groups.google.com/group/turbogears.
For more options, visit https://groups.google.com/d/optout.

Paul Kraus

unread,
Sep 15, 2015, 4:20:58 PM9/15/15
to TurboGears
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.

Moritz Schlarb

unread,
Sep 15, 2015, 4:41:51 PM9/15/15
to turbo...@googlegroups.com
On 15.09.2015 22:20, Paul Kraus wrote:
> Is there a way to easily turn a sqlalchemy model object into a
> dictionary/JSON?

That is actually something I really need(ed), too - I want to send
SQLAlchemy objects to Celery workers that have no access to the database.

Because I had some deadlines, I hacked something together myself quite
quickly:
https://github.com/moschlar/SAUCE/blob/feature/queue/sauce/lib/serialize.py
though I'm not overly happy with this.

But before that, I tried to evaluate libraries that are focused on
object serialization:

- http://docs.pylonsproject.org/projects/colander/en/latest/ and
http://colanderalchemy.readthedocs.org/en/latest/
- https://marshmallow.readthedocs.org/en/latest/ and
http://marshmallow-sqlalchemy.readthedocs.org/en/latest/

but neither of them really worked OOTB with my model.
You mileage may vary! ;) (Especially if you only need it in one
direction, a.k.a. as_dict!)

I also had a look at http://pyyaml.org/ because it (now at least) also
seems to handle object references pretty well so it might be a good
choice for circular references.

So if someone has some experience on this topic, I'd also be glad to
hear about it! ;)

Best wishes,
--
Moritz Schlarb

Alessandro Molina

unread,
Sep 16, 2015, 3:12:41 AM9/16/15
to TurboGears
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.

Alessandro Molina

unread,
Sep 16, 2015, 4:14:43 AM9/16/15
to TurboGears
In the past when I needed to pass data around services I ended up relying on sprox which has a dictify function:

        import sprox
        provider = sprox.sa.provider.SAORMProvider(session=DBSession, engine=None, metadata=None)
        user = DBSession.query(model.User).first()
        result = provider.dictify(user, omit_fields=['_password', 'password'])

But nowadays I just end up using json_encode and sending json back and forth as it is far easier and whatever service there is on the other end will probably decode it without much effort.

By the way if we are able to properly think of a design for a marshalling feature, that would be something that can be integrated in tg.JSONEncoder, sprox and tgext.crud as all of them provide their own marshalling to dictionaries and to json. It would make sense to have a single coherent behaviour for all of them.

--
Moritz Schlarb

Reply all
Reply to author
Forward
0 new messages