Opened 6 weeks ago

Last modified 2 weeks ago

#36066 assigned Cleanup/optimization

Document that Q objects can be used directly in annotations

Reported by: Laurent Bergeron Owned by: Lufafa Joshua
Component: Database layer (models, ORM) Version: 5.1
Severity: Normal Keywords: documentation
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: yes UI/UX: no
Pull Requests:19016

Description

I needed to annotate something to a queryset and found a solution that looked like this:

queryset = MyModel.objects.annotate(
    created_less_than_hour_ago=Q(time_of_creation__gt=(now() - timedelta(hours=1)))
)

It worked fine and "created_less_than_hour_ago" was set to True if the object was created less than an hour ago and to False otherwise. I found another way to achieve this though because I didn't find any documentation about the use of Q objects inside of the annotate method, so I couldn't understand why and how it worked.

So, here I am asking for documentation about it.

According to the ticket's flags, the next step(s) to move this issue forward are:

  • To improve the patch as described in the pull request review comments or on this ticket, then uncheck "Patch needs improvement".
  • If creating a new pull request, include a link to the pull request in the ticket comment when making that update. The usual format is: [https://github.com/django/django/pull/#### PR].

Change History (6)

comment:1 by Laurent Bergeron, 6 weeks ago

Type: UncategorizedNew feature

comment:2 by Sarah Boyce, 6 weeks ago

Summary: Documentation about the use of Q objects inside of the annotate method would be nice.Document that Q objects can be used directly in annotations
Triage Stage: UnreviewedAccepted
Type: New featureCleanup/optimization

I believe support for this was implemented in #27021
I agree that we could add something in the documentation specific for Q objects

comment:3 by Lufafa Joshua, 6 weeks ago

Owner: set to Lufafa Joshua
Status: newassigned

comment:4 by Lufafa Joshua, 5 weeks ago

comment:5 by Andrew Graham-Yooll, 3 weeks ago

Has patch: set

comment:6 by Sarah Boyce, 2 weeks ago

Patch needs improvement: set
Note: See TracTickets for help on using tickets.
Back to Top