基于以前的QuerySet

时间:2018-04-29 11:51:32

标签: django django-views django-queryset

这可能是一个简单的答案,但经过几天的搜索后,我似乎无法找到实现这一目标的正确方法。

我有一个模板,我想要显示与分配给用户的评估相关的所有问题。我以为我可以使用以下结果:

ResponseDetails = AssessmentResponse.objects.prefetch_related('assessment').filter(id=response_id)

通过查看对象并抓住assessment_id然后我可以将其作为过滤器传递到下一个查询集中,但我无法使其工作。

问题:因为视图没有根据AssessmentResponse模型中的assessment_id进行过滤,所以它给出了AssessmentQuestion模型中的每个问题。

答案可以让我实际上有一个良好的睡眠,试图找出它。

视图

def UpdateAssessmentResponse(request, response_id):
    ResponseDetails = AssessmentResponse.objects.prefetch_related('assessment').filter(id=response_id)

    QuestionList = AssessmentQuestion.objects.all()
    ChoiceList = AssessmentQuestionChoice.objects.all()
    context = {
        "ResponseDetails":ResponseDetails,
        "QuestionList":QuestionList,
        "ChoiceList": ChoiceList,
        #"ID" : ID,
    }
    return render(request, "assessment/assessment_response_update.html", context)

模板

    {% if QuestionList and ResponseDetails%}
      {% csrf_token %}
      {% for question in QuestionList %}
          <p> {{ question.question_text }} </p>
            {% for choice in ChoiceList %}
              {% if choice.question_id == question.pk %}
              <fieldset id="group1">
                <div class="custom-control custom-radio custom-control-inline">
                <input type="radio" class="custom-control-input" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
                <label class="custom-control-label" for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label>
                </div>
              <fieldset id="group1">
              {% endif %}
            {% endfor %}
      {% endfor %}
      <div class="card-footer">
      <input type="submit" value="Submit" class="btn btn-primary" />
      </div>
      {% else %}
          <p>There are currently no questions for this assessment.</p>
      {% endif %}

型号:

class AssessmentForm(models.Model):
    name = models.CharField(max_length=400)
    description = models.TextField()
    created = models.DateTimeField(auto_now_add=True)
    is_published = models.BooleanField()
    due_date = models.DateField(default=datetime.now, blank=True, null=True)

    def __str__(self):
        return self.name

class AssessmentResponse(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT)
    assessment = models.ForeignKey('AssessmentForm', on_delete=models.CASCADE)
    created = models.DateTimeField(auto_now_add=True)
    last_update = models.DateTimeField(auto_now=True)

    def get_absolute_url(self):
        return reverse('assessment_response_detail', args=[str(self.id)])

    #def get_assessment_id(self):
    #    return self.assessment

    def __str__(self):
        return self.user

class AssessmentQuestionType(models.Model):
    type = models.CharField(max_length=30)

    def __str__(self):
        return self.type

class AssessmentQuestionCategory(models.Model):
    name = models.CharField(max_length=200)
    assessment = models.ForeignKey('AssessmentForm', on_delete=models.CASCADE)

    def __str__(self):
        return self.name

class AssessmentQuestionSubCategory(models.Model):
    name = models.CharField(max_length=200)
    parent_category = models.ForeignKey('AssessmentQuestionCategory', on_delete=models.CASCADE)

    def __str__(self):
        return self.name

#CHOICES_HELP_TEXT = _(u"""The choices field is only used if the question type if the question type is 'radio', 'select', or 'select multiple' provide a comma-separated list of options for this question .""")

class AssessmentQuestion(models.Model):
    question_type = models.ForeignKey('AssessmentQuestionType', on_delete=models.PROTECT)
    question_text = models.TextField()
    is_required = models.BooleanField()
    category = models.ForeignKey('AssessmentQuestionCategory', on_delete=models.PROTECT, blank=True, null=True)
    subcategory = models.ForeignKey('AssessmentQuestionSubCategory', on_delete=models.PROTECT, blank=True, null=True)
    assessment = models.ForeignKey('AssessmentForm', on_delete=models.CASCADE)

    def __str__(self):
        return self.question_text

class AssessmentQuestionChoice(models.Model):
    question = models.ForeignKey(AssessmentQuestion, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200, blank=True, null=True)

    def __str__(self):
        return self.choice_text

class AssessmentAnswer(models.Model):
    text = models.CharField(max_length=1024)
    question = models.ForeignKey('AssessmentQuestion', on_delete=models.CASCADE)
    response = models.ForeignKey('AssessmentResponse', on_delete=models.PROTECT)

    def __str__(self):
        return self.text

1 个答案:

答案 0 :(得分:0)

想出来! Objects.filter是一个惰性查询,因此我的其他查询集实际上无法用作过滤器。通过使用objects.get来解决它。