Building a genealogical model

Hi everybody
I would like to build a genealogical model called Person in which a Person has a father and a mother
Father and Mother came from the same model Person

I need some recurse model to do this like this

Class Person (models.Model)
blah, blah, blah

 Father = models.ForeignKey('self',null=True,related_name='Person',on_delete=models.CASCADE)
 Mother = models.ForeignKey('self',null=True,related_name='Person',on_delete=models.CASCADE)

but I got some errors when I try to makemigrations

suivi.Person.Mother: (fields.E302) Reverse accessor for 'suivi.Person.Mother' clashes with field name 'suivi.Person.Mother'.
HINT: Rename field 'suivi.Person.Mother', or add/change a related_name argument to the definition for field 'suivi.Person.Mother'.

and the same about Father

I have no problem if I use just one field like Father but one parent is missing !!

So I don’t understant how I can define a model with 2 recurse fields from the same model
Thanks for your explantions, or web references and your help

The related_name fields must be unique within the model. You can’t use the same name for both Mother and Father.

Humm, But the fields mother and father point to the same object (Person) though, every Person has a father and a mother

I don’t understand how I must define this model, did you have some examples ?
Thanks for your help

The related_name does not refer to the class to which the foreign key points. That would be the first argument of models.ForeignKey, which in this case has to be 'self' as it points to the class Person itself (which in python it cannot do in the very same class’ definition, hence the 'self').

The argument value of related_name is instead used to define what the reverse accessor on the foreign class is called. So in this case, when you have a Person object called parent, you would access the child with parent.Person. Now, not only does this clash with the same related_name being used for the other relation, in my opinion it also makes little sense when trying to model familiy relations.

There certainly is a use case for a reverse accessor of the children of a Person, but it won’t work like that, as you could only ever have one child per Person. What you may want, although this depends on your use case, is two relations for mother and father, as you have already, without a reverse accessor and then another OneToMany relation that contains the children of a Person. I’d also remove the CASCADE, as this may produce unwanted behaviour, but it also might be fully intentional.

You should also be aware that pretty much any model for familiy relations is going to run into edge cases that you can’t model, when fed real world data, but again this depends on the use case.

Thanks for your explanations, now it’s clearer
I change the related_name to differents names and it works fine
Merci beaucoup

Idée a deux francs :

Keep the Person model.
Use Many to many relationship (using through) to keep track of the relationship between Person objects.

class Person(models.Model):
    name = models.CharField(...)
    family_ties = models.ManyToManyField('person.FamilyTie', through='FamilyTie')

class FamilyTie(models.Model):
    class Type(models.IntegerChoices):
        MUMMY = 1, 'Mummy'
        DADDY = 2, 'Daddy'

    type = models.PositiveSmallIntegerField('Type', choices=Type.choices, null=False, blank=False, default=Type.MUMMY)
    from = models.ForeignKey('person.Person', null=False, blank=False, on_delete=models.CASCADE, related_name='from')
    to = models.ForeignKey('person.Person', null=False, blank=False, on_delete=models.CASCADE, related_name='to')