views:

155

answers:

2

I have a model with ManyToManyField to another model. I would like to get all the info on a particular record (including the related info from other models) return by JSON.

How to get django-piston to display those values? I would be happy with just primary keys. Or can you suggest another option ?

A: 

I may be wrong, but this should do it:

class PersonHandler(BaseHandler):
    model = Person
    fields = ('id', ('friends', ('id', 'name')), 'name')

    def read(self, request):
        return Person.objects.filter(...)
David Wolever
Great! Absolutely wonderful! Thank you very much!
Jurica Zeleznjak
+1  A: 

You need to define a classmethod on the handler that returns the many-to-many data, I don't believe Piston does this automatically.

class MyHandler(BaseHandler):
    model = MyModel
    fields = ('myfield', 'mymanytomanyfield')

    @classmethod
    def mymanytomanyfield(cls, myinstance):
        return myinstance.mymanytomanyfield.all()
Daniel Roseman
Really? How does the classmethod on the handler get called? I haven't noticed any reference to that in the documentation… But, then, undocumented features don't really surprise me :(
David Wolever