【发布时间】:2018-05-18 07:42:31
【问题描述】:
我有两个这样的模型:
class McqQuestion(models.Model):
mcq_question_id = models.IntegerField()
test_id = models.ForeignKey('exam.Test')
mcq_right_answer = models.IntegerField()
class UserMcqAnswer(models.Model):
user = models.ForeignKey('exam.UserInfo')
test_id = models.ForeignKey('exam.Test')
mcq_question_id=models.ForeignKey('exam.McqQuestion')
user_answer = models.IntegerField()
我需要匹配 user_answer 和 mcq_right_answer。可以通过执行以下原始查询来做到这一点。
rightAns=UserMcqAnswer.objects.raw('SELECT B.id, COUNT(A.mcq_question_id) AS RightAns\
FROM exam_mcqquestion AS A\
LEFT JOIN exam_usermcqanswer AS B\
ON A.mcq_question_id=B.mcq_question_id_id\
WHERE B.test_id_id=%s AND B.user_id=%s AND\
A.mcq_right_answer=B.user_answer',[test_id,user_id])
1) 但问题是无法将结果作为 JSONResponse 传递,因为它说 TypeError: Object of type 'RawQuerySet' is not JSON serializable 2) 是否可以通过使用对象和过滤的查询集来替代这个原始查询?
【问题讨论】:
标签: json django django-queryset