1
0
mirror of https://github.com/django/django.git synced 2025-10-29 08:36:09 +00:00

Fixed #29497 -- Fixed loss of assigned parent when saving child with bulk_create() after parent.

This commit is contained in:
Hannes Ljungberg
2020-10-22 16:50:37 +02:00
committed by Mariusz Felisiak
parent 83a8da576d
commit 10f8b82d19
3 changed files with 65 additions and 34 deletions

View File

@@ -453,10 +453,12 @@ class QuerySet:
obj.save(force_insert=True, using=self.db)
return obj
def _populate_pk_values(self, objs):
def _prepare_for_bulk_create(self, objs):
for obj in objs:
if obj.pk is None:
# Populate new PK values.
obj.pk = obj._meta.pk.get_pk_value_on_save(obj)
obj._prepare_related_fields_for_save(operation_name='bulk_create')
def bulk_create(self, objs, batch_size=None, ignore_conflicts=False):
"""
@@ -493,7 +495,7 @@ class QuerySet:
opts = self.model._meta
fields = opts.concrete_fields
objs = list(objs)
self._populate_pk_values(objs)
self._prepare_for_bulk_create(objs)
with transaction.atomic(using=self.db, savepoint=False):
objs_with_pk, objs_without_pk = partition(lambda o: o.pk is None, objs)
if objs_with_pk: