[Django] #33242: Annotate a related field

8 views
Skip to first unread message

Django

unread,
Oct 29, 2021, 12:16:09 PM10/29/21
to django-...@googlegroups.com
#33242: Annotate a related field
-------------------------------------+-------------------------------------
Reporter: rossm6 | Owner: nobody
Type: New | Status: new
feature |
Component: Database | Version: 3.2
layer (models, ORM) |
Severity: Normal | Keywords: orm
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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

--
Ticket URL: <https://code.djangoproject.com/ticket/33242>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 30, 2021, 7:22:57 AM10/30/21
to django-...@googlegroups.com
#33242: Support annotating related fields with QuerySet.annotate().

-------------------------------------+-------------------------------------
Reporter: rossm6 | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: orm | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => wontfix


Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/33242#comment:1>

Reply all
Reply to author
Forward
0 new messages