Django: ManyToMany Relationship and unsaved objects

Django does come with a convenient database abstraction layer (DBAL) but unfortunately this DBAL is not very talkative. In case you get an error messages along the lines of

this boils down to: first save the parent object, then add the objects for many-to-many relations and then save the parent object again. If you do not save the object beforehand, you will get the error message above on assignment of the many-to-many relation’s objects.

Less talking, more code

For example, your could contain something similar to

Then you should use this code for inserting new FooClass objects:

Leave a comment

Your email address will not be published.

2 thoughts on “Django: ManyToMany Relationship and unsaved objects”