Opened 3 years ago

Closed 3 years ago

#33242 closed New feature (wontfix)

Support annotating related fields with QuerySet.annotate().

Reported by: rossm6 Owned by: nobody
Component: Database layer (models, ORM) Version: 3.2
Severity: Normal Keywords: orm
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I think it would be sensible to allow this -

m = Message.objects.annotate(room__something=Value('DOH', output_field=CharField())).select_related('room')

m.room.something == 'DOH' # is true

Perhaps the annotate API should accept a kwarg -

m = Message.objects.annotate(room__something=Value('DOH', output_field=CharField()), annotate_related=True).select_related('room')

m.room.something == 'DOH' # is true

https://stackoverflow.com/questions/69771898/is-there-a-way-to-annotate-a-related-field-in-django

Change History (1)

comment:1 by Mariusz Felisiak, 3 years ago

Resolution: wontfix
Status: newclosed
Summary: Annotate a related fieldSupport annotating related fields with QuerySet.annotate().

Thanks for the proposition. QuerySet.annotate()annotates each object in the QuerySet with the expression, so you can use .annotate(room_something=...) and refer it in the main object. IMO adding support for annotating related-objects would make the API unnecessarily complicated without any clear benefits.

You can raise the idea on the DevelopersMailingList to reach a wider audience and see what other think.

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