Opened 10 years ago
Closed 9 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 , 10 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:2 by , 9 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
Related to "
QuerySet.values()should be aliasable (#16735)" and django-developers discussion.