Add an object by id in a ManyToMany relation in Django Add an object by id in a ManyToMany relation in Django python python

Add an object by id in a ManyToMany relation in Django


Although the documentation of the add method does not mention it, you can actually pass directly an id to it, like this:

my_instance.add(id)

Surely, this only works when the primary key is used as the identifier for the relation (the default behaviour).