Creating many objects using django-polymorphic

Viewed 979

I'm using django-polymorphic and have a base class with about 8 derived classes. It works great except when I try to create many objects at once, in this case the performance is very poor. My code looks like this:

class Parent(PolymorphicModel):
  ...
class DerivedA(Parent):
  ...
class DerivedB(Parent):
  ...

@transaction.commit_on_success
def create_objects()
  for model_class in (DerivedA, DerivedB...):
    instance = model_class(...)
    instance.save()

I've also tried using Parent.objects.bulk_create, but this doesn't work well with polymorphic-django, because it just creates the base object and not the child objects. When I try to use bulk_create of each of the child models, it raises ValueError("Can't bulk create an inherited model").

Is there a more efficient way of creating many polymorphic objects?

1 Answers
Related