多次加入django?

Multiple join in django?

我在 django 2.1 中有四个模型 Quiz、Assignment、Question 和 Option。我想检索特定于测验的问题和选项。我尝试使用代码

**a=Quiz.objects.all() 
print(a[0].assignment_set.all())**

通过使用上面的代码我得到了赋值集:

[
{'id': 1, 'questionValue': 1, 'Question_id': 1, 'Quiz_id': 1}, 
{'id': 2, 'questionValue': 1, 'Question_id': 2, 'Quiz_id': 1}
] 

但我不知道如何获得问题的相应选项。

我的模特

class Quiz(models.Model):
    name = models.CharField(max_length=30)
    type = models.CharField(max_length=30)
    openDate = models.DateField()
    closeDate = models.DateField()
    maxMarks= models.IntegerField()
    isActive= models.BooleanField()
    noOfAttempts=models.IntegerField()
    noOfQuestions=models.IntegerField()

class Question(models.Model):
    name = models.CharField(max_length=30)
    description=models.TextField()
    Category=models.ForeignKey(Category,on_delete=models.CASCADE)

class Option(models.Model):
    name = models.TextField()
    Question=models.ForeignKey(Question,on_delete=models.CASCADE)
    value= models.IntegerField()

class Assignment(models.Model):
    questionValue=models.IntegerField()   
    Question=models.ForeignKey(Question,on_delete=models.CASCADE)
    Quiz=models.ForeignKey(Quiz,on_delete=models.CASCADE)

我想获取如下格式

"assignmentSet": [
      {
        "Question": {
          "id": "1",
          "name": "Q 1",
          "optionSet": [
            {
              "name": "option 1"
            },
            {
              "name": "Option 2"
            },
            {
              "name": "Option 3"
            },
            {
              "name": "Option 4"
            }
          ]
        }
      },
      {
        "Question": {
          "id": "2",
          "name": "Q 2",
          "optionSet": [
            {
              "name": "Option 1"
            },
            {
              "name": "Option 2"
            },
            {
              "name": "Option 3"
            },
            {
              "name": "Option 4"
            }
          ]
        }
      }]

试试这个 听说你会得到问题的所有选项

class Question(models.Model):
    name = models.CharField(max_length=30)
    description=models.TextField()
    Category=models.ForeignKey(Category,on_delete=models.CASCADE)

    @property
    def options(self):
        return self.option_set.values()