[Django] #28078: Could not use fileld as part of expression when annotate key shadows model field

6 views
Skip to first unread message

Django

unread,
Apr 14, 2017, 6:59:31 AM4/14/17
to django-...@googlegroups.com
#28078: Could not use fileld as part of expression when annotate key shadows model
field
-------------------------------------+-------------------------------------
Reporter: Pavel | Owner: nobody
Patrin |
Type: | Status: new
Uncategorized |
Component: Database | Version: 1.10
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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')),
)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28078>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 14, 2017, 9:21:36 AM4/14/17
to django-...@googlegroups.com
#28078: Could not use fileld as part of expression when annotate key shadows model
field
-------------------------------------+-------------------------------------
Reporter: Pavel Patrin | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

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

--
Ticket URL: <https://code.djangoproject.com/ticket/28078#comment:1>

Django

unread,
Apr 15, 2017, 9:01:55 AM4/15/17
to django-...@googlegroups.com
#28078: Can't use field as part of expression when annotate key shadows model field
-------------------------------------+-------------------------------------
Reporter: Pavel Patrin | Owner: nobody
Type: | Status: new
Cleanup/optimization |

Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* type: Uncategorized => Cleanup/optimization
* stage: Unreviewed => Accepted


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/28078#comment:2>

Django

unread,
Aug 23, 2022, 11:53:24 PM8/23/22
to django-...@googlegroups.com
#28078: Can't use field as part of expression when annotate key shadows model field
-------------------------------------+-------------------------------------

Reporter: Pavel Patrin | Owner: nobody
Type: | Status: closed

Cleanup/optimization |
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* status: new => closed
* resolution: => invalid


Comment:

After further review I'm going to close this one as invalid.

If you shadow a field with an annotation (this seems to be only allowed
here because `values` is used prior see #28072) then the ORM will default
to the annotation override, `F` doesn't mean field reference it should
have been name `Ref` (and `Ref` should have been named `ResolvedRef`).

If you want to keep referring to `MyModel.amount_field_1` you must create
an alias to it prior to shadowing it with your annotation

{{{#!python
MyModel.objects.annotate(amount_field_1_alias=F("amount_field_1")).values(
"grouping_field"
).annotate(
amount_field_1=Sum("amount_field_1"),
amount_field_2=Sum(F("amount_field_1_alias") + F("amount_field_2")),
)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28078#comment:3>

Reply all
Reply to author
Forward
0 new messages