> > On Sep 10, 3:41 pm, waugust <[email protected]> wrote:
> > > This is driving me nuts..
> > > I have a route mapped as an extra collection on a resource:
> > > maps.resource('post', 'posts', collection={'json':'GET'})
>
> > > The controller action:
> > > @jsonify
> > > def json(self):
> > >  q = sa.query(Post)
> > >         count = q.count()
> > >         posts = q.all()
>
> > >         items = []
> > >         for post in posts:
> > >             _post =
> > > {'id':post.id,'caption':post.title,'description':post.content}
> > >             items.append(_post)
>
> > >         postsJson = {'posts': count,'items':items}
>
> > >         return postsJson
>
> > > Returns null... the code in a shell returns the structure fine (the
> > > data set isn't empty).
>
> > > What makes this even stranger is that if I were to map this as a
> > > member, and pass it an Id - I get a response as expected (even though
> > > i didn't change the code, other then adding the id parameter).
>
> > > Anybody know what's going on?
> > > When trying app.get(url('json_posts')) in the shell I get "<Response
> > > 200 OK ''>" (null).
> > > I also get that same response if I put an invalid url in there
> > > (instead of a 404)...
> > > The url('json_posts') resolves correctly and paster routes reports it
> > > appropriately as well.
>
> On Sat, Sep 11, 2010 at 5:45 PM, Wyatt Baldwin
> <[email protected]>wrote:
>
> > I'm not really sure what's going on there, but when you use
> > map.resource, you can do this instead of adding a special `json`
> > action:
>
> >    GET /post/1.json
>
> > Then 'json' will be passed as the `format` arg to the appropriate REST
> > action. In this example:
>
> >    def show(self, id, format='html'):
> >        # id is '1' and format is 'json'
> >        # ...
> >        if format == 'json':
> >            # convert member to json and return it
>
>
On Sep 11, 5:55 pm, Wojtek Augustynski <[email protected]> wrote:
> Thanks, for the response. So doing it that way (other then the decorator
> way) i just change the mime type and use simplejson.dumps?

Well, there are various approaches you could take, but I'd add another
method that specifically handles returning JSON responses:

    def show(self, id, format='html'):
        entity = Session.query(Entity).get(id)
        if format == 'json':
            return self._render_json(entity)

    @jsonify
    def _render_json(self, entity):
        # I'd put this in a controller base class
        # convert `entity` to some kind of object that can be
JSONified, probably a dict
        return object_that_can_be_jsonified

Note, this just shows the general approach I would take. For example,
this doesn't deal with collections. If you want to see what I
*actually* do, look here and check out the various `_render` methods:

    http://code.google.com/p/restler/source/browse/restler/controller.py

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" 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/pylons-discuss?hl=en.

Reply via email to