[Answered ]-Django setting many_to_many object while doing a bulk_create

2👍

From the docs https://docs.djangoproject.com/en/1.9/ref/models/querysets/#bulk-create:

If the model’s primary key is an AutoField it does not retrieve and set the primary key attribute, as save() does.

It does not work with many-to-many relationships.

bulk_create literally will just create the objects, it does not retrieve the PK into the variable as save does. You would have to re-query the db to get your newly created objects, and then create the M2M relationships, but it sounds like that would not be appropriate and that your current method is currently the best solution.

Leave a comment