7

Sorry for the none-descriptive title, but I didn't really have a clue as to how to word this.

Say I have two models as such:

class Person(...):
  name = ... #have an attribute

class Family(...):
  mum = models.OneToOneField(Person)
  dad = models.OneToOneField(Person)

When I have a family containing mum and dad, I would think calling dad.family would yield me the family dad is in. However, I get an error message saying that this clashes with the mum attribute. The solution here is to use relative_names. But calling the family from mums side something else than from dads feels weird for me. WHy can't I just call dad.family? Could someone explain to me what exactly is clashing here?

Thanks!

1
  • I suppose the above is just an example for OneToOneFields. But if it's a snippet from from some real-world code, just be prepared that your software will most probably have to handle families with varying number of parents (usually 1-2), of any gender combination. Also if "mum" and "dad" is defined as "being a parent of gender G", it could be more useful storing gender information in the Person object, so you don't have to manually move a Person from "mum" to "dad" if gender information is updated.
    – pereric
    May 26, 2021 at 10:39

2 Answers 2

7

The problem is that, given your model, a Person could be a mum to one Family, and a dad to a different family.

In that case, a query like this would be ambiguous:

pat = Person.objects.get(name='Pat')
family1.mum = pat
family1.save()

family2.dad = pat
family2.save()

pat.family # Which family do we want?

For that reason, you need to define a reverse relation name for each one (family_as_mum and family_as_dad, for instance)

5
  • 1
    Thank you so much for your answer. But, does a OneToOneField not guarantee that this doesn't happen?
    – user827080
    Oct 3, 2012 at 16:19
  • No, the OneToOneField only guarantees uniqueness within that field. So all of the mums are distinct, and all of the dads are distinct, but nothing stops a person from being both. Oct 3, 2012 at 16:22
  • Guess I should read up more on the exact definition of a one to one relationship then. Thanks again.
    – user827080
    Oct 3, 2012 at 16:24
  • 2
    If you're familiar with databases, think of it as a foreign key with a 'UNIQUE NOT NULL' constraint -- In your case, you have two columns, each UNIQUE, but they're independent of each other. Oct 3, 2012 at 16:29
  • 2
    I like the way Django treats LGBT
    – Qback
    Sep 28, 2018 at 12:22
2

I know the answer above does answer the question of "why is it clashing", and I know it is an old question. However, I came across it trying to come up with a way to have the code restrict the entries in one column based on the entries in the other. I did not find a sufficient answer, but managed to puzzle it out myself. Below is an answer to a question that isn't directly asked, but may be helpful to others.

from django.db import models

class Person(models.Model):
  name = models.CharField(max_length=255)

class Family(models.Model):
  mum = models.OneToOneField(Person, on_delete=models.SET_NULL, related_name='family_as_mum', null=True)
  dad = models.OneToOneField(Person, on_delete=models.SET_NULL, related_name='family_as_dad', null=True)

  def save(self, *args, **kwargs):
    if Family.objects.filter(mum=self.dad).exists() or Family.objects.filter(dad=self.mum).exists() or self.mum == self.dad:
      return #any person can only be a mum or a dad, not both
    else:
      super(Family, self).save(*args, **kwargs)

  def update(self, *args, **kwargs):
    if Family.objects.filter(mum=self.dad).exists() or Family.objects.filter(dad=self.mum).exists() or self.mum == self.dad:
      return #same as save, don't want them to update a family member in an invalid way
    else:
      super(Family, self).update(*args, **kwargs)

  @staticmethod
  def get_family(person):
    if Family.objects.get(mum=person).exists():
      return Family.objects.get(mum=person)
    elif Family.objects.get(dad=person).exists():
      return Family.objects.get(dad=person)
    else:
      return None

*note, found the @staticmethod here, thought it might be useful to have a function to find the family associated with a parent, since we may not know up front if a person is a mum or a dad

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.