Skip to content Skip to sidebar Skip to footer

Django Setting Many_to_many Object While Doing A Bulk_create

I am using Django 1.9 and am trying the bulk_create to create many new model objects and associate them with a common related many_to_many object. My models are as follows #Compu

Solution 1:

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.


Post a Comment for "Django Setting Many_to_many Object While Doing A Bulk_create"