import json from pprint import pprint from random import shuffle, randint from collections import Counter from django.core.management.base import BaseCommand from university.models import OnlineExamQuestion class Command(BaseCommand): def handle(self, *args, **options): # exam = {} # with open('pcta_exam.json', 'r') as f: # exam = json.loads(f.read()) # questions = [] # for section in exam: # for index, q in enumerate(section['questions']): # db = OnlineExamQuestion.objects.get(id=q['id']) # questions.append([q['id'], section['title'], index + 1, q['problem'], db.tag]) # questions = sorted(questions, key=lambda x: x[4]) # for q in questions: # print('【{}】 试卷第{}道, 数据库id:{}, tag:{}'.format(q[1], q[2], q[0], q[4])) # print('{}\n'.format(q[3])) for id in [18, 27]: db = OnlineExamQuestion.objects.get(id=id) print('\033[32m-------------------->{}: 第{}题, 数据库索引: {}<----------------------\033[0m'. format(db.tag, db.comment, db.id)) print(db.problem) for o in db.body: print('{}: {}'.format(o['value'], o['text'])) _right_answer = db.answer if isinstance(db.answer, str) else ', '.join(db.answer) print('正确答案: {}'.format(_right_answer)) print('\n\n')