# Building a genealogical model

**URL:** <https://forum.djangoproject.com/t/building-a-genealogical-model/12623>\
**Category:** Using the ORM\
**Created:** [March 15, 2022, 2:19pm UTC](https://forum.djangoproject.com/t/building-a-genealogical-model/12623 "2022-03-15T14:19:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thegritche](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/thegritche/32/2956_2.png) [@Thegritche](https://forum.djangoproject.com/u/Thegritche)\
**Post date:** [March 15, 2022, 2:19pm UTC](https://forum.djangoproject.com/t/building-a-genealogical-model/12623/1 "2022-03-15T14:19:46Z")

</div>

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

```auto
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

```auto
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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 15, 2022, 3:00pm UTC](https://forum.djangoproject.com/t/building-a-genealogical-model/12623/2 "2022-03-15T15:00:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Thegritche](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/thegritche/32/2956_2.png) [@Thegritche](https://forum.djangoproject.com/u/Thegritche)\
**Post date:** [March 16, 2022, 6:52am UTC](https://forum.djangoproject.com/t/building-a-genealogical-model/12623/3 "2022-03-16T06:52:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eott](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/eott/32/10533_2.png) [@eott](https://forum.djangoproject.com/u/eott)\
**Post date:** [March 16, 2022, 8:43am UTC](https://forum.djangoproject.com/t/building-a-genealogical-model/12623/4 "2022-03-16T08:43:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Thegritche](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/thegritche/32/2956_2.png) [@Thegritche](https://forum.djangoproject.com/u/Thegritche)\
**Post date:** [March 17, 2022, 6:06am UTC](https://forum.djangoproject.com/t/building-a-genealogical-model/12623/5 "2022-03-17T06:06:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![plopidou](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/plopidou/32/5148_2.png) [@plopidou](https://forum.djangoproject.com/u/plopidou)\
**Post date:** [March 17, 2022, 9:37am UTC](https://forum.djangoproject.com/t/building-a-genealogical-model/12623/6 "2022-03-17T09:37:14Z")

</div>

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.

```auto
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')

```
