Opened 40 hours ago
Last modified 27 hours 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: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
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 (3)
comment:1 by , 40 hours ago
Type: | Uncategorized → New feature |
---|
comment:2 by , 31 hours 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: | Unreviewed → Accepted |
Type: | New feature → Cleanup/optimization |
comment:3 by , 27 hours ago
Owner: | set to |
---|---|
Status: | new → assigned |
Note:
See TracTickets
for help on using tickets.
I believe support for this was implemented in #27021
I agree that we could add something in the documentation specific for Q objects