@Gael: Interesting idea, I hadn't thought about that. Though the 
no-arguments aspect might be a bit of a problem.

The approach of attaching the schema to the request in the view-function:
request.marshmallow_schema = UserOutputSchema()

... has another advantage besides allowing arguments: You can pick a 
different schema depending on the request. For example, with a 
GET-parameter like expand=True, you could pick the schema that serializes 
relationships as well. Without that GET-parameter, you pick the simple 
schema that doesn't serialize the relationships.

@Jonathan: I was imagining something along those lines; that sounds good. 
Ideally I'd like to extend/inherit from pyramid's JSON-renderer, because 
then I could keep the add_adapter(..) - functionality (which I find very 
handy). I'm seeing 2 options. In both cases, you attach the schema to the 
request:

request.marshmallow_schema = UserOutputSchema()

Based on that, you either

a) extend/inherit from Pyramid's JSON-renderer and make it use the schema 
(when present).
b) implement a __json__-method for the SQLAlchemy declarative Base-class 
and make that use the schema (when present).

I'll have to think about which one fits more cleanly.

How do other people deal with this? This is something that should come up 
for many people: How do you serialize your SQLAlchemy-objects to JSON? A 
very simple __json__-method does the trick initially. But what do you do, 
when you need to serialize certain fields/relationships for some requests 
but not for others?


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

Reply via email to