I'm using multi-table inheritance models.
from django.db import models
class Place(models.Model):
name = models.CharField(max_length=50)
address = models.CharField(max_length=80)
class Restaurant(Place):
serves_hot_dogs = models.BooleanField(default=False)
serves_pizza = models.BooleanField(default=False)
class Cinema(Place):
sells_tickets = models.BooleanField(default=False)
sells_popcorn = models.BooleanField(default=False)
class Coffee(Place):
sells_tea = models.BooleanField(default=False)
I have a view that creates several different models:
items = [
Restaurant(...),
Restaurant(...),
Restaurant(...),
Cinema(...),
Cinema(...),
Coffee(...),
Coffee(...),
# + 1.000 other items
]
for item in items:
item.save()
Obviously, this is really inefficient since it creates a lot of queries. Unfortunately, Django doesn't provide a bulk-create method for multi-table inheritance yet (there is an open pull request for it). What is the best way to optimize this code? Do I have to write a raw SQL query or is there another way?