def post(self): data = request.get_json() quiz_id = data['url'].split('/')[-1] # split "/quiz/10" by "/" and get "10". answers_correct_row = [quiz for quiz in Choices.select(Choices.id).dicts().join(QuizQuestions).group_by() .where((Choices.is_correct == True) & (QuizQuestions.quiz_id == quiz_id))] answers_correct_db = {id_['id'] for id_ in answers_correct_row} answers_user = {int(v) for k, v in data.items() if k != 'url'} correct_answers_count = len(answers_correct_db) - len(answers_user.difference(answers_correct_db)) percent = (100 * correct_answers_count)//len(answers_user) return jsonify({'score': percent})