Django - 有效地批量创建继承的模型

WAF*_*WAF 9 mysql inheritance django-models database-performance python-2.7

Python 2.7.9 Django 1.7 MySQL 5.6

我想填充属于多个类的一大堆对象实例,将它们堆叠成一个类似create()的查询,打开数据库连接,执行查询,然后关闭.我的主要动机是性能,但代码紧凑性也是一个优点.

功能bulk_create()似乎正是我想要的,但我违反了至少一个这里列出的警告,即

它不适用于多对多关系.

它不适用于多表继承方案中的子模型.

这些限制也在源代码中描述:

# 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
# child tables which references this. There are two workarounds, 1)
# this could be implemented if you didn't have an autoincrement pk,
# and 2) you could do it by doing O(n) normal inserts into the parent
# tables to get the primary keys back, and then doing a single bulk
# insert into the childmost table. Some databases might allow doing
# this by using RETURNING clause for the insert query. We're punting
# on these for now because they are relatively rare cases.
Run Code Online (Sandbox Code Playgroud)

但是当我尝试它时返回的错误是通用的

ValueError:无法批量创建继承的模型

我的模型显然不包含任何多对多字段或外键.我不完全清楚他们所指的是多表继承方案,所以我不确定这是不是我的问题.我希望我可以使用看起来像这样的结构,但后来我得到了一般错误,所以没有骰子:

child class with OneToOneField---\
                                  \   
child class with OneToOneField----->---concrete parent class
                                  /
child class with OneToOneField---/
Run Code Online (Sandbox Code Playgroud)

至于源代码中提出的变通方法,#1对我来说不是一个选项,#2看起来并不吸引人,因为我认为它会牺牲我正在寻求的性能提升.

bulk_create()在处理这样的继承时是否有其他可以模拟的变通方法并且不会放弃性能提升?我需要回到原始SQL吗?我不介意为每个子对象类型创建一个单独的集合并执行单独的INSERT/ create().

WAF*_*WAF 10

我确定的解决方法是将我收集到create()的所有内容都包含在内with transaction.atomic():.通过在所有Python都返回之前不打开任何数据库连接或执行任何查询,这大大减少了运行时间.

缺点可能是,如果遇到任何错误,则回滚所有更改并且数据库不受影响.这可以通过将create()s分成批次并打开和关闭每个事务周围的事务来解决.(在我的情况下,这不是理想的行为,因为我想要所有数据或者没有数据.)