Opened 22 months ago

Last modified 22 months ago

#28078 new Cleanup/optimization

Can't use field as part of expression when annotate key shadows model field

Reported by: Pavel Patrin Owned by: nobody
Component: Database layer (models, ORM) Version: 1.10
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Could not use fileld as part of expression when annotate key shadows model field

For example i have a model

    class MyModel(Model):
        grouping_field = TextField()
        amount_field_1 = IntegerField()
        amount_field_2 = IntegerField()


    queryset = MyModel.objects.values('grouping_field')

    # This is ok!
    queryset.annotate(amount_field_1=Sum('amount_field_1'))

    # This triggers error!
    # {FieldError}Cannot compute Sum('<CombinedExpression: F(amount_field_1) + F(amount_field_2)>'): '<CombinedExpression: ...>' is an aggregate
    queryset.annotate(
        amount_field_1=Sum('amount_field_1'),
        amount_field_2=Sum(F('amount_field_1') + F('amount_field_2')),
    )  

Change History (2)

comment:1 Changed 22 months ago by Simon Charette

While the exception is different this looks similar to #28072.

comment:2 Changed 22 months ago by Tim Graham

Summary: Could not use fileld as part of expression when annotate key shadows model fieldCan't use field as part of expression when annotate key shadows model field
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

I'm not sure what should be done here, accepting for further investigation.

Note: See TracTickets for help on using tickets.
Back to Top