Opened 9 years ago

Closed 8 years ago

#25871 closed New feature (fixed)

Allow expressions in values() queryset method

Reported by: Josh Smeaton Owned by: Ian Foote
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: expressions
Cc: josh.smeaton@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

It should be possible to insert expressions directly into a values() method, rather than first annotating and then selecting in values().

# Proposed
Model.objects.values(complex=SomeExpression()).aggregate(Sum('field'))

# Current
Model.objects.annotate(complex=SomeExpression()).values('complex').aggregate(Sum('field'))

The major hurdle here is allowing **kwargs into values() to support the alias. That's an addition to the API so should be backwards compatible, but it'll also probably rule out using various state based kwargs like flat=True and their ilk in the future. We could do some juggling to ensure we're only processing expression kwargs as expressions by checking if they have the attribute resolve_expression.

Change History (4)

comment:1 by Tim Graham, 9 years ago

Triage Stage: UnreviewedAccepted

Related to "QuerySet.values() should be aliasable (#16735)" and django-developers discussion.

comment:2 by Ian Foote, 8 years ago

Owner: changed from nobody to Ian Foote
Status: newassigned

comment:3 by Ian Foote, 8 years ago

Has patch: set
Last edited 8 years ago by Tim Graham (previous) (diff)

comment:4 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: assignedclosed

In 39f35d4b:

Fixed #25871 -- Added expressions support to QuerySet.values().

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