# Model Relationship

**URL:** <https://forum.djangoproject.com/t/model-relationship/6943>\
**Category:** Using Django\
**Created:** [March 9, 2021, 3:30am UTC](https://forum.djangoproject.com/t/model-relationship/6943 "2021-03-09T03:30:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![clarkeustaquio](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/clarkeustaquio/32/6132_2.png) [@clarkeustaquio](https://forum.djangoproject.com/u/clarkeustaquio)\
**Post date:** [March 9, 2021, 3:30am UTC](https://forum.djangoproject.com/t/model-relationship/6943/1 "2021-03-09T03:30:53Z")

</div>

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

For instance, we have an employee model

```auto
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:

```auto
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.

```auto
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.

```auto
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.

```auto
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!

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 9, 2021, 1:05pm UTC](https://forum.djangoproject.com/t/model-relationship/6943/2 "2021-03-09T13:05:51Z")

</div>

> [@clarkeustaquio](#):
>
> 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.

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](https://docs.djangoproject.com/en/3.1/ref/models/relations/): `group_task.project_set.all()`.
