How can I iterate over ManyToManyField?

Goro picture Goro · Mar 5, 2012 · Viewed 30.7k times · Source

A simple question and yet I can't find an answer for it.

I have a model with a ManyToMany field:

class Stuff(models.Model):
  things = models.ManyToManyField(Thing)

then in a different function I want to do this:

myStuff = Stuff.objects.get(id=1)
for t in myStuff.things.all:
  # ...

But that is giving me:

TypeError: 'instancemethod' object is not iterable

How can I iterate over a manyToManyField ?

Answer

Abid A picture Abid A · Mar 6, 2012

Try adding the () after all: myStuff.things.all()