user2080105
user2080105

Reputation: 1702

Django association between three models

I have these three models :

Using Django's models system, how to represent the fact that a person is participating in a particular project with a specific role ?

general problem : What is the proper way to handle "ternary associations" using Django ?

Upvotes: 5

Views: 872

Answers (1)

wanaryytel
wanaryytel

Reputation: 3482

I would do it using an intermediary model for m2m relationship and add a field there.
Something like this:

class Role(models.Model):
    name = models.CharField(max_lenth=32)

class Project(models.Model):
    name = models.CharField(max_lenth=32)

class PersonProject(models.Model):
    person = models.ForeignKey('.Person')
    project = models.ForeignKey(Project)

    role = models.ForeignKey(Role)

class Person(models.Model):
    projects = models.ManyToManyField(Project, through=PersonProject)

Upvotes: 2

Related Questions