Hi,
I have a function that returns projectName
, QuestionnaireTitle
and finalScore
The function loops through every project
and every questionnaire.
I’m trying to format the data so that i end up with a list or a dict that contains the projectName and the scores for each questionnaire so that i can pass this into a JSChart.
The format of the chart is:
name: 'Project Name',
data: [5,8,4,7,2,5,5,7]
My current queryset is returning
[{'q_rounded': 100, 'title': 'Critical Fundamentals', 'final_score': 5.0, 'project': <Project: ProjectName>},...]
Is there a way to take the existing queryset and construct the data in the way i need, or should is the a better way within the function?
for project in all_projects:
for q in questionnaires:
print("getting ", q.title, "responses for", project.name, project.id)
if ProjectQuestionnaireResponse.objects.filter(project_name_id=project.id, questionnaire_id = q.id).exists():
q_response = ProjectQuestionnaireResponse.objects.get(project_name_id=project.id, questionnaire_id = q.id)
q_answered = ProjectQuestionnaireAnswer.objects.filter(response = q_response, answer__isnull=False).count()
if q_answered > 1:
q_count = (100 / ProjectQuestionnaireQuestion.objects.filter(questionnaire_id = q.id).count() * q_answered)
else:
q_count = 0
q_rounded = (5 * round(q_count / 5))
# Scoring Calulator #
# Get Questionnaire Range
total_questions =ProjectQuestionnaireQuestion.objects.filter(questionnaire_id = q.id).count()
score_range = (4 * total_questions)
green = 2
red = -2
green_answer_value = (ProjectQuestionnaireAnswer.objects.filter(response = q_response,answer__choice_value="Green").count() * green)
red_answer_value = (ProjectQuestionnaireAnswer.objects.filter(response = q_response,answer__choice_value="Red").count() * red)
total_score = (green_answer_value + red_answer_value
if total_score is not None:
final_score = round(((2 * total_questions + total_score) / score_range) * 10,2)
else:
pass
results.append({'q_rounded':q_rounded,'title':q.title,'final_score':final_score,"project":project})
else:
q_rounded = 0
results.append({'q_rounded':q_rounded,'title':q.title,"project":project})
Thanks