[Fixed]-How to Design my data model in django with many OneToMany relations

1👍

If a Person can have multiple Specialities, then you can implement a oneToMany relationship, just by adding a foreignKey:

 class Speciality(models.Model):
    name = models.CharField()
    code = models.CharField()
    person =  models.ForeignKey(Person)

However, if you have several Persons with the same specialities (let’s say 10 Persons are Painters), you would have 10 different Painters entries.

To avoid duplication, you can implement a ManyToMany relationship :

 class Speciality(models.Model):
    name = models.CharField()
    code = models.CharField()
    person =  models.ManyToManyField(Person)

Django will create a third table in your DB to handle that (you don’t need to write your PersonSpeciality class).

👤Pixou

Leave a comment