Opened 3 years ago

Last modified 3 years ago

#28072 new New feature

Allow QuerySet.annotate() to use the name of an exisiting field

Reported by: Marcin Nowak Owned by: nobody
Component: Database layer (models, ORM) Version: 1.8
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

Because of this exception https://github.com/django/django/blob/master/django/db/models/query.py#L900 I can't add annotation.
I need to >rename< db_field within the query and fetch results by .values() / .values_list(), so mapping db values to fields is not required.

Queries like "select x as y, y as x from table" are valid, and as a Django user I want to do similar things using queryset API, for my own responsibility.

Please add possibility to bypass the constraint somehow, or move this constraint elsewhere and activiate it when no .values()/.values_list() are used. Also please backport the change to the 1.8.x branch.

Thank you.

Change History (2)

comment:1 Changed 3 years ago by Tim Graham

Summary: Cannot annotate exisiting fieldAllow QuerySet.annotate() to use the name of an exisiting field
Triage Stage: UnreviewedAccepted
Type: BugNew feature

I'm not sure if this is feasible or a good idea, but if you provide a patch, we can take a look. Per our supported versions policy, it won't be backported to 1.8.

comment:2 Changed 3 years ago by Marcin Nowak

Please note that .extra(select={'x': 'y'}) works without limitation and can be used as a workaround for .annotate()'s conflict exception.

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