Writing a controller method that can generate either JSON or a web page that
represents a single row from a single table.
Seems as though I need a slightly different return from the method depending on
whether I want to generate JSON or a web page if I'm to get clean JSON and yet
make the templates work. Or I have to somewhat generate the JSON, but that's
clearly not idiomatic.
It seems as though for the JSON, I want to do
d = obj.__dict__
del d['__sa_instance_state__']
return d
Weirdly clunky. Seems like there ought to be a cleaner way to convert a
SQLAlchemy object to a dictionary or directly to JSON. Happy to hear there is a
cleaner way, but this is all I could work out. Anyway.
The problem is that I can't see how to get at dict returned directly to be
visible within the template. It's buried inside context, but there's a ton of
other crap in there also. If there's a way to get the return from the
controller method, well I can't find it.
I could just return a dict from my controller method ('return dict(obj=d)') but
this isn't the JSON I want to generate: I don't want it all wrapped in a
needless level that needs to be dereferenced each time.
So: I either need some way to determine what is wanted
(request.header['Accept'] doesn't help if the type is requested in the URL
http://mysite.com/foo.json) or some missing piece of this puzzle filled in for
me, because it's almost beautifully simple but it's actually really ugly. :-)
--
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en.