Problem
I am using the django-model-utils InheritanceManager. I have a super Notification(models.Model) class which I use to create many notification subclasses such as PostNotification(Notification)
, CommentNotification(Notification)
, etc., and when trying to run CommentNotification.objects.bulk_create(list_of_comment_notification_objects)
, i get the following traceback:
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
and upon inspecting the query.py file, we get this causes the error:
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")
Environment Django Model Utils version: 3.1.1 Django version: 1.11.7 Python version: 2.7.3
Example
PostNotification.objects.bulk_create(
[PostNotification(related_user=user, post=instance) for user in users]
)
throws the above exception
What I have tried and though was a success originally:
I though that simply running:
BaseClass.objects.bulk_create(list_of_SubClass_objects)
instead of SubClass.objects.bulk_create(list_of_SubClass_objects)
would work and return a list of SubClass values, but subsequently running SubClass.objects.all()
would return an empty result. The bulk_create() would only create a Notification base class object for each item in the list.