I think you are confusing ManyToManyField
and ForeignKey
To initialize a Foreign key, you would just do:
i = A().save()
i.b = objectb
i.save()
where objectb
is an instance of class B
The reason i.b.add(objectb)
throws an exception is,
i.b
is accessing the attribute b
of the model instance i
, which is None
, and when the ORM tries to call the add
on b
, it is indeed None
. Hence the error.