Why is this always returning zero

Am i right in thinking that self.question.choice_set.all(): returns the related objects to the fk to question = models.ForeignKey(ProjectQuestionnaireQuestion, on_delete=models.CASCADE) but this is not what i need. But instead i should get the values from self.answer.choice_value... ?

You’re trying to get a Choice object. More specifically, you want to use a specific choice_value and choice_weight.

You’re trying to find the Choice object that matches the answer.

This means your query is going to start out looking like:

Happy Birthday, Ken. and thanks for your help again.

I think I got it now.

    def score(self):
        if not self.answer:
            return 0
        choice = Choice.objects.get(choice_text=self.answer)
        if choice == self.answer:
            return choice_value_to_score_map.get(choice.choice_value, 0) * (choice.choice_weight)
            print('No match')               
        return 0

and then I am getting the values in my view like this:

            all_q_answered = ProjectQuestionnaireAnswer.objects.filter(response = q_response, answer__isnull=False)
            questionnaire_score = []
            for answer in all_q_answered:
                total_score = answer.score

Seems to work. But anything here you think could be improved?

“Could it be improved?” Yes. Does it need to? No.

For the purposes of your further edification, you could convert this:

into a single query that calculates the scores in the query and returns the values as a list.

(If your end result is to calculate the sum of all questionnaire_score and not just return a list of individual values, then you would use aggregation to perform the calculation and not annotate the individual scores.)

Side note: If you’re trying to match up those individual scores being collected in questionnaire_score to specific questions, you will need to order the results of the query to ensure you get those answers in a defined order. A queryset without some order specification returns results in an indeterminant order.