On Aug 30, 4:16 pm, Idan Gazit <
i...@pixane.com> wrote:
> On Aug 30, 2:33 pm, jespern <
jno...@gmail.com> wrote:
>
> > class SomeFunkyHandler(BaseHandler):
> > fields = ('id', 'field1', 'field2', 'user') # user is NOT a field,
> > it'a method
> > user = user_info
>
> Ok, beginning to get the hang of things. Am feeling the generic
> resource method love, but the following recipie doesn't work as soon
> as I add nesting to the mix:
>
> def bar_info(cls, bar):
> return { 'id':
bar.id, 'url': bar.url, ..., 'username':
> bar.user.username }
>
> class FooHandler(BaseHandler):
> fields: ('id', 'field1', 'field2', ('bars', ('bar_info')))
>
> It appears (from adding some print statements) that bar_info() is
> never called in this case. A little playing reveals that generic
> resource methods are called just fine when they're not nested.
Rigth, resource methods and "fields" are mutually exclusive, they
can't be post-processed by nesting. That's by design.
We'd need something new for that, some sort of "proxy" method, but
there's nothing like that currently. Patches welcome ;-)
> I can work around this by doing something like:
>
> def foo_bars_info(cls, foo):
> bars = []
> for bar in foo.bars:
> bars.append( {'id':
bar.id, ..., 'username':
> bar.user.username })
> return bars
>
> fields: ('id', 'field1', 'field2', 'foo_bars_info')
>
> On the right track? Or am I doing it wrong?
That's not a work-around, that's the correct way to do it. On a side
note, you could probably "yield" here, as we'll flatten in the
serializer.
Jesper