Opened 7 years ago

Closed 7 years ago

#28277 closed Cleanup/optimization (fixed)

Validate that annotate() and aggregate() args and kwarg values are expressions

Reported by: Tim Graham Owned by: Mariusz Felisiak
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The current error messages are unhelpful. For example:

>>> Book.objects.aggregate(alias=Max('price'), output_field=FloatField())
...
  File "/home/tim/code/django/django/db/models/sql/query.py", line 974, in add_annotation
    annotation = annotation.resolve_expression(self, allow_joins=True, reuse=None,
AttributeError: 'FloatField' object has no attribute 'resolve_expression'

or

>>> Book.objects.annotate(FloatField()))
  File "/home/tim/code/django/django/db/models/query.py", line 890, in annotate
    if arg.default_alias in kwargs:
AttributeError: 'FloatField' object has no attribute 'default_alias'

Change History (3)

comment:1 by Mariusz Felisiak, 7 years ago

Owner: changed from nobody to Mariusz Felisiak
Status: newassigned

comment:2 by Mariusz Felisiak, 7 years ago

Has patch: set

comment:3 by GitHub <noreply@…>, 7 years ago

Resolution: fixed
Status: assignedclosed

In 6e228d0b:

Fixed #28277 -- Added validation of QuerySet.annotate() and aggregate() args.

Thanks Tim Graham and Nick Pope for reviews.

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