{{{#!python
class Parent(models.Model):
name = models.TextField()
class Child(models.Model):
name = models.TextField()
parent = models.ForeignKey(Parent, on_delete=models.RESTRICT)
}}}
Now if we have some function that defines a bunch of these objects to
later commit them in bulk:
{{{#!python
def build_objects(parent_child_mapping):
parents = []
children = []
# {"father": ["son", "daughter"], ...}
for parent_name, child_names in parent_child_mapping.items():
parent = Parent(name=parent_name)
parents.append(parent)
children.extend(Child(parent=parent, name=child_name) for
child_name in child_names)
# now commit all objects in bulk
Parent.objects.bulk_create(parents)
# fails with IntegrityError: parent_id is not nullable
Child.objects.bulk_create(children)
}}}
I would expect the above to work fine, given the parent ID's are known
when inserting the children, however this would throw an `IntegrityError`
because `parent_id` on the `Child` objects is still `None`.
Adding a simple hack to the function will let it work fine, but it's
annoying to do this ''everywhere'' where this pattern of creating objects
is followed.
{{{#!python
def build_objects(parent_child_mapping):
parents = []
children = []
for parent_name, child_names in parent_child_mapping.items():
parent = Parent(name=parent_name)
parents.append(parent)
children.extend(Child(parent=parent, name=child_name) for
child_name in child_names)
# now commit all objects in bulk
Parent.objects.bulk_create(parents)
# add hack to make sure object IDs are properly assigned
for child in children:
child.parent_id = child.parent.id
# now this call can succeed
Child.objects.bulk_create(children)
}}}
This seems like something that should be supported by Django, and I
believe this would make the ORM more consistent overall.
Thanks.
--
Ticket URL: <https://code.djangoproject.com/ticket/32190>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* type: New feature => Bug
* resolution: => duplicate
Comment:
Duplicate of #29497. It was fixed in
10f8b82d195caa3745ba37d9424893763f89653e (Django 3.2+).
--
Ticket URL: <https://code.djangoproject.com/ticket/32190#comment:1>