mirror of
https://github.com/django/django.git
synced 2025-09-23 10:42:45 +00:00
Fixed #24997 -- Enabled bulk_create() on proxy models
This commit is contained in:
parent
fedef7b2c6
commit
9a5cfa05a0
6 changed files with 68 additions and 18 deletions
|
@ -411,7 +411,7 @@ class QuerySet(object):
|
|||
Inserts each of the instances into the database. This does *not* call
|
||||
save() on each of the instances, does not send any pre/post save
|
||||
signals, and does not set the primary key attribute if it is an
|
||||
autoincrement field.
|
||||
autoincrement field. Multi-table models are not supported.
|
||||
"""
|
||||
# So this case is fun. When you bulk insert you don't get the primary
|
||||
# keys back (if it's an autoincrement), so you can't insert into the
|
||||
|
@ -423,13 +423,18 @@ class QuerySet(object):
|
|||
# this by using RETURNING clause for the insert query. We're punting
|
||||
# on these for now because they are relatively rare cases.
|
||||
assert batch_size is None or batch_size > 0
|
||||
if self.model._meta.parents:
|
||||
raise ValueError("Can't bulk create an inherited model")
|
||||
# Check that the parents share the same concrete model with the our
|
||||
# model to detect the inheritance pattern ConcreteGrandParent ->
|
||||
# MultiTableParent -> ProxyChild. Simply checking self.model._meta.proxy
|
||||
# would not identify that case as involving multiple tables.
|
||||
for parent in self.model._meta.get_parent_list():
|
||||
if parent._meta.concrete_model is not self.model._meta.concrete_model:
|
||||
raise ValueError("Can't bulk create a multi-table inherited model")
|
||||
if not objs:
|
||||
return objs
|
||||
self._for_write = True
|
||||
connection = connections[self.db]
|
||||
fields = self.model._meta.local_concrete_fields
|
||||
fields = self.model._meta.concrete_fields
|
||||
objs = list(objs)
|
||||
self._populate_pk_values(objs)
|
||||
with transaction.atomic(using=self.db, savepoint=False):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue