I am working on a django project and have difficulty in relating my data models.
I wish to achieve the following, I have 3 models Employee, Project and ProjectRole.
I wish to implement the models in such a way that User1 will have Lead (ProjectRole) in project1 but will have a Member (ProjectRole) in project2. While User2 will have Lead (ProjectRole) in project2 but Member (ProjectRole) in project1. Maybe the following table will make it clear.
Employee Project ProjectRole
User1 project1 Lead
User1 project2 Member
User2 project1 Member
User2 project2 Lead
I am totally confused and wish that someone explain in simple terms how relationships are to be defined within the models.
So far, I have the following in my models.py, but I am sure it is incorrect, since it is not working as expected.
class ProjectRole(models.Model):
rolename = models.CharField(max_length=10)
class Project(models.Model):
projname = models.CharField(max_length=10)
projrole = models.ManyToManyField(ProjectRole)
class Employee(models.Model):
empname = models.CharField(max_length=10)
projrole = models.ManyToManyField(ProjectRole)
Would love some guidance on how to achieve this seemingly complex design.
Use a third model:
the third model will thus contains triples of
employee,projectandrole.If you want to easily get the
Employees for aProject, by looking at theEmployementmodel, we can spanManyToManyFields that go through theEmploymentmodel with:but if you need the full details, you can just use
my_project.employment_set.all()to get theEmploymentrecords that thus contain also the details regarding the.rolefor that.employee.