我正在使用Django 2.0
我有以下查询字符串
question = ChapterQuestion.objects.filter(chapter__course=course_learn.course).first()
options = ChapterQuestion.objects.filter(
chapter__course=course_learn.course
).exclude(
pk=question.pk
).order_by('?').all()[:3]
现在,我想将question
实例合并到options
QuerySet中,并对其顺序进行randoze以生成4个选项。
如何将实例添加到同一模型的QuerySet中?
答案 0 :(得分:0)
它不漂亮,但有点像......?
x = random.randInt(4)
randQuestions = []
for count in range(x): randQuestions.add(options[count])
randQuestions.add(question)
for count in range(x,3): randQuestions.add(options[count])