title: Many-to-Many Relationships through 3-way Legacy Junction Table in Django
body: <p>With a legacy database where the many-to-many relationships between projects, the people in the projects, and their roles in that project are represented through a three-way junction table like so:</p>
  
  <p><a href="https://i.stack.imgur.com/4WybA.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4WybA.jpg" alt="data relationship"></a> </p>
  
  <p>How would one represent this data in with Django models?</p>
  
  <p>I'm trying to write a model where <code>project.manager</code> would return a list of <code>person</code> instances that are paired with the <code>manager</code> role for that <code>project_id</code>. So far, the closest I've gotten is using <code>models.ManyToManyField</code> through two of different fields in <code>Rolepersonproject</code>, but that only gives me the roles and people associated with that project, not what roles those people played. How would I do this? Is this a job for custom managers, the ManyToManyField, model methods, or custom SQL?</p>
  
  <p><code>manage.py inspectdb</code> gave me model classes like these to work with:</p>
  
  <pre><code>class Project(models.Model):
      project_id = models.AutoField(db_column='project_id', primary_key=True)
  
  class Role(models.Model):
      role_id = models.AutoField(db_column='role_id', primary_key=True)
  
  class Person(models.Model):
      person_id = models.AutoField(db_column='person_id', primary_key=True)
  
  class Rolepersonproject(models.Model):
      project_id = models.ForeignKey(Project, models.DO_NOTHING, db_column='project_id')
      role_id = models.ForeignKey(Role, models.DO_NOTHING, db_column='role_id')
      person_id = models.ForeignKey(Person, models.DO_NOTHING, db_column='person_id')
  </code></pre>