Using a multi-table inheritance model as a through table

I’m trying to use a multi-table inheritance model as a through table, as shown below:

class Base(models.Model):
  ...

class Through(Base):
  ...

class RelatedModel(models.Model):
  ...

class Model(models.Model):
  field = models.ManyToManyField(RelatedModel, through='Base')


instance = Model.objects.get(pk=1)
related = RelatedModel.objects.get(pk=1)
instance.add(related)  # ValueError: Can't bulk create a multi-table inherited model

However, I keep running into an error when adding a model to the ManyToMany relationship, as to how multi-table inheritance models cannot be bulk created. Is there any way around this? ManyToManyField.add doesn’t take a bulk argument so it doesn’t seem to be possible to disable bulk creation there.

Does anyone know of a workaround for this without getting rid of the inheritance?

Actually, add is a bulk operation. It uses the *args notation to accept all positional parameters as a list. (*objs in this specific case) See the .add method docs.

In other words, you can do something like instance.add(r1, r2, r3, r4), inserting all values at once. (See the various examples at Many-to-many relationships | Django documentation | Django)

Since you’re using a through table here, you could just create the table instances directly - assuming:

class Base(...):
    model = ForeignKey(Model, ...)
    related_model = ForeignKey(RelatedModel, ...)
    ...

Then, you can establish your relationship as:

instance = Model.objects.get(pk=1)
related = RelatedModel.objects.get(pk=1)
base = Base(model=instance, related_model=related, ...)
base.save()