Opened 3 years ago

Last modified 2 months 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


Because of this exception 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 (4)

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.

comment:3 Changed 6 months ago by Marcin Nowak

Thank you. But please note that there is no possibility to use expressions with extra(select={..}).

comment:4 Changed 2 months ago by David

For what it's worth this would be really handy. I took a look, but adding this kind of functionality is likely beyond me.

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