Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating a team of users with jobs

I want to make a model where users can create a team of users, and in these teams add the job that they did and choose recipient of the job(that is the member of this team) that they did it for.

I don’t how to create a choice field for the recipient of the job.

from django.db import models
from django.contrib.auth.models import User

class Team(models.Model):
    name = models.CharField(max_length=120)
    members = models.ManyToManyField(User)

class Job(models.Model):
    belonging = models.ForeignKey(Team,on_delete=models.CASCADE)
    executor = models.ForeignKey(User,on_delete=models.CASCADE)
    task_description = models.CharField(max_length=180)
    recipient = models.ManyToManyField(Team.members) # <--

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It will depend on the logic of your project. If a Job can be used by several users then you need ManyToManyField:

class Job(models.Model):
    recipient = models.ManyToManyField(User) # <--

On the other hand if a Job can be destined to only one user it will be ForeignKey:

class Job(models.Model):
    recipient = models.ForeignKey(User) # <--
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading