Home > Software engineering >  Several properties with the same type Django
Several properties with the same type Django

Time:09-19

I have two models

class Manager(models.Model):
name = models.CharField(max_length=250)

def __str__(self):
    return self.name

class Project(models.Model):
title = models.CharField(max_length=250, null=True, blank=True)
work_statement = models.TextField(null=True, blank=True)
contract_id = models.CharField(max_length=250, null=True, blank=True)
note = models.TextField(null=True, blank=True)
customer = models.CharField(max_length=250, null=True, blank=True)
#executer = models.ManyToManyField(Manager)
deadline = models.DateTimeField(null=True, blank=True)

So there is logic I want Model Project to has several managers as executers but not all of them how can i do it?

so for example: Project "N" can include two managers for execution

i know that this way is not correct

class Project(models.Model):
title = models.CharField(max_length=250, null=True, blank=True)
work_statement = models.TextField(null=True, blank=True)
contract_id = models.CharField(max_length=250, null=True, blank=True)
note = models.TextField(null=True, blank=True)
customer = models.CharField(max_length=250, null=True, blank=True)
executer1 = models.ForeignKey(Manager)
executer2 = models.ForeignKey(Manager) 
deadline = models.DateTimeField(null=True, blank=True)

CodePudding user response:

you must use manyToMany

Read the following documents

djangoproject

sankalpjonna

CodePudding user response:

You can use ManyToMany field with through. For example in your case, you can use the following code:

class Manager(models.Model):
    name = models.CharField(max_length=250)

    def __str__(self):
        return self.name

class Project(models.Model):
    ...
    executer = models.ManyToManyField(Manager, through='Membership')

class Membership(models.Model):
    manager = models.ForeignKey(Manager)
    project = models.ForeignKey(Project)
    role = models.CharField()  # or a foreignkey to Role model
  • Related