General Doubt in using annotations

I’m facing an issue, where I need to calculate an average based on some conditions, like this

for x in query:
            if (
                x.domain_specific_assessment is None
                and x.basic_program_assessment is not None
                and x.mini_project is None
            ):
                x.average_marks = x.basic_program_assessment
            elif (
                x.domain_specific_assessment is not None
                and x.basic_program_assessment is not None
                and x.mini_project is None
            ):
                x.average_marks = (x.basic_program_assessment / 100) * 5 + (
                    x.domain_specific_assessment / 100
                ) * 95
            elif (
                x.domain_specific_assessment is not None
                and x.basic_program_assessment is None
                and x.mini_project is None
            ):
                x.average_marks = x.domain_specific_assessment
            else:
                x.average_marks = None

        print(query[0].average_marks)
        print(query[1].average_marks)
        print(query[2].average_marks)
        print(query[3].average_marks)
        print(query.values("average_marks"))
        

where average_marks is an annotated field, it gets updated but when I try to print the value of average_marks as a whole it goes back to the default value. The output I received is 55.0 32.0 67.0 None <QuerySet [{'average_marks': 289}, {'average_marks': 291}, {'average_marks': 304}, {'average_marks': 315}]>
Yes I know I can do it with Case() like this

average_marks=Case(
                    When(
                        Q(domain_specific_assessment__isnull=False)
                        & Q(basic_program_assessment__isnull=False)
                        & Q(mini_project__isnull=False),
                        then=(F("mini_project") / 100) * 25
                        + (F("basic_program_assessment") / 100) * 5
                        + (F("domain_specific_assessment") / 100) * 70,
                    ),
                    When(
                        Q(domain_specific_assessment__isnull=True)
                        & Q(basic_program_assessment__isnull=False)
                        & Q(mini_project__isnull=True),
                        then=F("basic_program_assessment"),
                    ),
                    When(
                        Q(domain_specific_assessment__isnull=False)
                        & Q(basic_program_assessment__isnull=True)
                        & Q(mini_project__isnull=True),
                        then=F("domain_specific_assessment"),
                    ),
                    When(
                        Q(domain_specific_assessment__isnull=False)
                        & Q(basic_program_assessment__isnull=False)
                        & Q(mini_project__isnull=True),
                        then=(F("basic_program_assessment") / 100) * 5
                        + (F("domain_specific_assessment") / 100) * 95,
                    ),
                    default=None,
                ),

But it takes an enormous amount of time to complete (about 10-20 seconds)

How many rows are you processing where it’s taking roughly 15 seconds?

(Also, other than this reported duration issue, I’m not sure I understand what you’re looking for from us here.)

Hi, I’ve solved the issue(It is not an issue to begin with). I was wondering whether it is possible to change the values of the annotated fields in Queryset().

For example: I changed the annotated field average_marks in the above example,

x.average_marks = x.domain_specific_assessment

But when I tried accessing it back, it gave the default value.

You can change it - and as long as you continue to access the same objects, you will get the modified values. But if you force the queryset to be reevaluated, you will get the original values.