Multiple table inheritance subclass with an additional OneToOneField relationship to the parent class

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Multiple table inheritance subclass with an additional OneToOneField relationship to the parent class

Brian Maissy
I have a model which in essence 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 generate the migration, I get the following error:

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

It looks like Django is promoting the wife OneToOneField to be the parent_link relationship. But that's not what I'm trying to do. I want MarriedMan to have the default implicit OneToOneField for its parent class Person record, plus an extra OneToOneField for its wife.

As far as I was able to understand from the docs, the way to specify that this is not my intention is by not passing parent_link=True.

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 manually specify the parent_link relationship in addition to the wife field, but that doesn't seem like the correct solution.

Am I missing something?

Thanks.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fd82f238-b141-4283-8d14-20969a9855b9%40googlegroups.com.