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.
On Tue, Sep 15, 2015 at 10:41 PM, Moritz Schlarb <[email protected]>
wrote:
> 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
>
> --
> 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.