Model Relationship

Hi, I need some help for database relationship and assignment.

For instance, we have an employee model

class EmployeeModel(models.Model):
         email = models.EmailField(max_length=150, unique=True)
         employee_no = models.IntegerField()
         first_name = models.CharField(max_length=50)
         last_name = models.CharField(max_length=50)
         middle_name = models.CharField(max_length=50)
 
         def __str__(self):
             return str(self.first_name + ' ' + self.last_name)

And let say an employee can have a group of task. But before that, we have to create a task in order to group it.

This is a task model:

class Task(models.Model):
         id = models.AutoField(primary_key=True)
         task_code = models.CharField(max_length=150, 
                                     unique=True, editable=False)
         name = models.CharField(max_length=150)
         type = models.CharField(max_length=20, editable=False)
         description = models.CharField(max_length=250)

         def __str__(self):
             return self.name

We then now group the tasks.

class GroupTask(models.Model):
         id = models.AutoField(primary_key=True)
         name = models.CharField(max_length=150)
         group_code = models.CharField(max_length=150,
                                      unique=True, editable=False)
         task = models.ManyToManyField(Task, blank=True)
 
         def __str__(self):
             return self.name

So after grouping, we now have a list of task in group task model. For example, in Group 1, we have Task 1, and Task 2.

This group of task should also be in a project, so we have to connect this group of task in a project model. Because each projects should have a group of task.

class Project(models.Model):
        code = models.CharField(max_length=20, editable=False, 
                               default=auto_generated_code)
        name = models.CharField(max_length=100)
        description = models.CharField(max_length=250)
        group_task = models.ManyToManyField(GroupTask, blank=True)

        def __str__(self):
            return str(self.name)

We are now able to add a grouptask in a project. But at this moment, we have not assign any group of task to an employee. So we have to create a separate model for that.

class TaskAssignment(models.Model):
        employee = models.OneToOneField(Employee, on_delete=models.CASCADE)
        group_task = models.ManyToManyField(GroupTask)
        
        def __str__(self):
            return self.employee.last_name + ', ' + self.employee.first_name

So my question now is, since we are assigning a grouptask to an employee, how can i determine what project this grouptask is in?

For example, i assign Group 1 to ‘Employee Test’.

And this ‘Group 1’ is in a Project 1, and Project 2, because each ‘GroupTask’ can be use to multiple projects.

But i only want to assign Group 1 in Project 1, how can i do this? Because at this moment, i am only assigning grouptask to an employee, but i don’t know yet how can i determine what project this grouptask is in.

Thank you!

A GroupTask can be associated with many Project. Given your structure, there’s no such relationship as “GroupTask with Project 1”. If this is a requirement, you may need to rethink your data structure. In particular, you might want to consider whether or not “GroupTask X” assigned to Project 1 is really the same GroupTask as the GroupTask assigned to Project 2.

To answer your other question, given a GroupTask named group_task, the list of Projects associated with it is accessed through a RelatedManager: group_task.project_set.all().

1 Like