[Django]-Understanding Django model inheritance quirks

2👍

The inheritance relationship introduces links between the child model and each of its parents (via an automatically-created OneToOneField).

In your case that means that with defining Bravo, Alpha gets propery named “bravo”.

See Multi-table inheritance and Inheritance and reverse relations documentation.

4👍

As far as I can tell, this is a consequence of the fact that the parent class has an implicit foreign key to all its descendants, which is in turn inherited by the children. That probably shouldn’t happen in fact, but there may not be any way of preventing it.

One possibility might be to explicitly set the parent link on Bravo so that its related_name is something other than ‘bravo’, so that you can reuse that name in Charlie.

class Bravo(Alpha):
    two = models.PositiveIntegerField()
    alpha = models.OneToOneField(Alpha, parent_link=True, related_name='not_bravo')

Leave a comment