I know that the answer above answers the question “why does it collide”, and I know that this is an old question. However, I stumbled upon this while trying to find a way to limit the number of records in one column based on the records in another. I did not find a sufficient answer, but I managed to figure it out myself. Below is the answer to a question that is not directly asked, but may be useful 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=SET_NULL, related_name='family_as_mum') dad = models.OneToOneField(Person, on_delete=SET_NULL, related_name='family_as_dad') 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
* pay attention, I found @staticmethod here, I thought it would be useful to have a function for finding a family associated with a parent, since we may not know the front if the person is mom or dad
source share