Search code examples
pythondjangomany-to-many

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

#Computational Job object
class OT_job(models.Model):
    is_complete = models.BooleanField()
    is_submitted = models.BooleanField()
    user_email = models.EmailField()

#Many sequences
class Seq(models.Model):
    sequence=models.CharField(max_length=100)
    ot_job = models.ManyToManyField(OT_job)

I have thousands of Seq objects that are submitted and have to be associated with their associated job. Previously I was using an iterator and saving them in a for loop. But after reading realized that Django 1.9 has bulk_create.

Currently I am doing

DNASeqs_list = [] 
for a_seq in some_iterable_with_my_data:
    # I create new model instances and add them to the list
    DNASeqs_list.append(Seq(sequence=..., ))

I now want to bulk_create these sequence and associate them with the current_job_object.

created_dnaseqs = Seq.objects.bulk_create(DNASeqs_list)
# How do I streamline this part below
for a_seq in created_dnaseqs:
    # Had to call save here otherwise got an error
    a_seq.save()
    a_seq.ot_job.add(curr_job_obj)

I had to call "a_seq.save()" in for loop because I got an error in the part where I was doing "a_seq.ot_job.add(curr_job_obj)" which said

....needs to have a value for field "seq" before this many-to-many relationship can be used.

Despite reading the other questions on this topic , I am still confused because unlike others I do not have a custom "through" model. I am confused with how best to associate the OT_Job with many Seqs with minimal hits to database.


Solution

  • 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.