Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

Auto fill up Django model

I'm building an application that displays tests included in a project. User can set the test result of each tests. My model contain 3 basic classes:

Requirement - in this class i want to hold every tests. Something like template table.

class Requirement(models.Model):
    requirement_name = models.CharField(max_length=256, default=None)
    nist = models.CharField(max_length=10, blank=True, null=True, default=None)
    cwe = models.CharField(max_length=10, blank=True, null=True, default=None)

Project - contain all projects created by users

class Project(models.Model):
    project_name = models.CharField(max_length=256, default=None)
    date_made =models.DateTimeField(auto_now_add=True)
    owner = models.ForeignKey(User, default=None, on_delete=models.CASCADE)
    requirements = models.ManyToManyField(Requirement, through='ReqsProject')

ReqsProject - in this table i want to store all of requirements that are stored in class Requirements

class ReqsProject(models.Model):
    project = models.ForeignKey(Project, default=None, on_delete=models.CASCADE)
    requirement = models.ForeignKey(Requirement, default=None, on_delete=models.CASCADE)
    status = models.BooleanField(default=True, blank=True)
    result = models.BooleanField(default=True, blank=True)

    class Meta:
        unique_together = [['project', 'requirement']]

Because of atributes "status" and "result" i can't use ManyToManyField. How can I automaticly fill up table ReqsProject with every Requirement_ids and special one project_id ? This might be something like this:

ID Project ID Requirement ID Status Result
1 1 1 - -
2 1 2 - -
3 1 3 - -
4 1 4 - -
... .... .... .. ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...