【发布时间】:2018-09-24 08:57:46
【问题描述】:
问题
我正在使用 django-model-utils InheritanceManager。我有一个超级 Notification(models.Model) 类,我用它来创建许多通知子类,例如 PostNotification(Notification)、CommentNotification(Notification) 等,当尝试运行 CommentNotification.objects.bulk_create(list_of_comment_notification_objects) 时,我得到以下回溯:
File "/home/me/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/models/query.py", line 429, in bulk_create
raise ValueError("Can't bulk create a multi-table inherited model")
ValueError: Can't bulk create a multi-table inherited model
在检查 query.py 文件后,我们发现这会导致错误:
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")
环境 Django 模型工具版本:3.1.1 Django 版本:1.11.7 Python 版本:2.7.3
示例
PostNotification.objects.bulk_create(
[PostNotification(related_user=user, post=instance) for user in users]
)
抛出上述异常
我已经尝试过,虽然最初是成功的:
我虽然只是运行:
BaseClass.objects.bulk_create(list_of_SubClass_objects) 而不是 SubClass.objects.bulk_create(list_of_SubClass_objects) 将起作用并返回子类值列表,但随后运行 SubClass.objects.all() 将返回空结果。 bulk_create() 只会为列表中的每个项目创建一个 Notification 基类对象。
【问题讨论】:
-
错误信息和documentation 都清楚地表明您不能在这种情况下使用
bulk_create。所以就别用了吗?
标签: django django-models django-model-utils