Opened 3 months ago

Closed 4 weeks ago

Last modified 4 weeks ago

#36066 closed Cleanup/optimization (fixed)

Document that Q objects can be used directly in annotations

Reported by: Laurent Bergeron Owned by: Samruddhi Dharankar
Component: Database layer (models, ORM) Version: 5.1
Severity: Normal Keywords: documentation
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no
Pull Requests:19219 merged, 19016 unmerged

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.

Change History (11)

comment:1 by Laurent Bergeron, 3 months ago

Type: UncategorizedNew feature

comment:2 by Sarah Boyce, 3 months 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, 3 months ago

Owner: set to Lufafa Joshua
Status: newassigned

comment:4 by Lufafa Joshua, 3 months ago

comment:5 by Andrew Graham-Yooll, 2 months ago

Has patch: set

comment:6 by Sarah Boyce, 2 months ago

Patch needs improvement: set

comment:7 by Samruddhi Dharankar, 5 weeks ago

Hi, I'm not sure if work is still being done on this. But I submitted a PR for the requested changes.

comment:8 by Sarah Boyce, 4 weeks ago

Owner: changed from Lufafa Joshua to Samruddhi Dharankar
Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:9 by Sarah Boyce <42296566+sarahboyce@…>, 4 weeks ago

Resolution: fixed
Status: assignedclosed

In 9120a19:

Fixed #36066 -- Documented that Q objects can be used directly in annotations.

comment:10 by Sarah Boyce <42296566+sarahboyce@…>, 4 weeks ago

In 930d9c2:

[5.2.x] Fixed #36066 -- Documented that Q objects can be used directly in annotations.

Backport of 9120a19c4ecb643111b073dd1069e6b410a03c23 from main.

comment:11 by Sarah Boyce <42296566+sarahboyce@…>, 4 weeks ago

In ccd5867a:

[5.1.x] Fixed #36066 -- Documented that Q objects can be used directly in annotations.

Backport of 9120a19c4ecb643111b073dd1069e6b410a03c23 from main.

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