[Answered ]-Django NameError when using cross reference in ManyToManyField

2👍

Have a look at https://docs.djangoproject.com/en/dev/topics/db/examples/many_to_many/.
The ManyToManyField must be present in only one model, then an implicit set is created in the other model and can be reached with the suffix _set. You will have:

class Model1(models.Model):
    field2 = models.ManyToManyField(Model2)

class Model2(models.Model):
    pass  

a = Model1()
b = Model2()
a.field2.add(b)

# Access the sets with:
a.field2.all()
b.model1_set.all()

Leave a comment