0

I have 2 models in my app -

In models/parent.py I have -

 from django.db import models
 class Parent(models.Model): 
       class Meta:
          db_table = "parent_table"
       start_date = models.DateField()
       end_date = models.DateField()

In models/child.py I have -

from django.db import models
from models.parent import Parent
class Child(models.Model): 
   class Meta:
      db_table = "child_table"
   some_ref = models.ForeignField(Parent)

We can do single insert into child table using the parent table reference as -

self.child_set.create(**args)

Is there a way by which we can do bulk insert into related table, as the normal bulk_create

DUDE_MXP
  • 724
  • 7
  • 24
  • Are the objects already stored in the database? – Willem Van Onsem May 22 '18 at 20:41
  • 1
    Slightly different from the question you asked (and maybe I'm misunderstanding), but I just set the related object myself and then use `bulk_create`. Something like `new_children = [Child(*child_args, parent=me) for child_args in SOME_LIST]` followed by `Child.objects.bulk_create(new_children)`. – RishiG May 22 '18 at 20:54

0 Answers0