how to access m2m through table fields

57 views
Skip to first unread message

harry389

unread,
May 31, 2011, 11:04:15 PM5/31/11
to django-piston
here are the model i have:
class A(Model):
foo = IntegerField()

class C(Model):
b = ForeignKeyField(B)
a = ForeignKeyField(A)
bar = IntegerField()

class B(Model):
foos = ManyToManyField('A', through='C')

if i wanted to display objects of class C (ie i want to display 'bar'
field of C objects), how should i change my handlers?

Michael Blume

unread,
Jun 13, 2011, 6:45:34 PM6/13/11
to django...@googlegroups.com
So the API read you're discussing is on C directly, and you want a list of C objects with only their 'bar' field displayed? I think simply including fields=('bar',) should do the trick. Am I missing something?

-Mike


--
You received this message because you are subscribed to the Google Groups "django-piston" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-pisto...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-piston?hl=en.


Hari Prasad

unread,
Jun 13, 2011, 8:33:06 PM6/13/11
to django-piston
the way i am doing this is by adding a property to class B like:
class B(Model):
   foos = ManyToManyField('A', through='C')
 
   def get_foos(self):
      return C.objects.filter(b=self)

and in the handler for B put the 'get_foos' in the fields

class BHandler(BaseHandler):
   fields = ('get_foos')

- Hari

On Tue, May 31, 2011 at 8:04 PM, harry389 <harr...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages