multiplate table inheritance subclass with extra OneToOneField relationship to parent class

58 views
Skip to first unread message

Brian Maissy

unread,
Oct 17, 2019, 4:42:21 PM10/17/19
to Django users
I have a model which essentially boils down to this:

class Person(models.Model):
name = models.TextField()


class MarriedMan(Person):
wife = models.OneToOneField(Person, on_delete=models.CASCADE,
related_name='husband')

But when I try to make the migration, I get the following error:

django.core.exceptions.ImproperlyConfigured: Add parent_link=True to webapp.MarriedMan.wife.

It seems like Django is automatically promoting my OneToOneField to be the one which holds the reference to the parent class. But that's not what I intended. I want there to be an extra wife relationship in addition to the implicit automatic OneToOneField.

As far as I could tell from the documentation, that's exactly what NOT passing parent_link=True is supposed to signify:

If you want to control the name of the attribute linking back to the parent, you can create your own OneToOneField and set parent_link=True to indicate that your field is the link back to the parent class.

As a workaround I could explicitly define the parent_link relationship plus my own wife relationship, but that doesn't seem to be the correct solution.

Am I missing something?

Thanks.

shubham vashisht

unread,
Jul 19, 2020, 7:00:48 PM7/19/20
to Django users
Did you find any solution?

Brian Maissy

unread,
Jul 20, 2020, 2:42:40 AM7/20/20
to Django users
I'm still using the workaround I mentioned above
Reply all
Reply to author
Forward
0 new messages