def load_test id test Test objects defer description get id id qs Ques

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def load_test(id):
test = Test.objects.defer('description').get(id=id)
qs = Question.objects.filter(test=test)
if test.question_random:
qs = qs.order_by('?')
if test.question_count != 0:
qs = qs[:test.question_count]
questions = list(qs)
qs = Variant.objects.filter(question__in=questions)
if test.variant_random:
qs = qs.order_by(question).order_by('?')
variants = {}
for item in qs:
if item.question_id not in variants:
variants[item.question_id] = []
variants[item.question_id].append(item)
return {'test' : test, 'questions': questions, 'variants': variants}