Opened 5 years ago
Last modified 5 years ago
#31860 closed Bug
"__date" in F expression not working, have to use Func() — at Version 2
| Reported by: | Aleksandr Smechov | Owned by: | nobody |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 3.1 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
When filtering a query for two equal datetime fields, and using __date, the __date in the F expression remains a timestamp.
For example:
Author.objects.filter(
Q(articles__isnull=False)
& Q(articles__date_published__date=F("articles__date_edited__date"))
)
This will return an empty set, as F("articlesdate_editeddate") remains a timestamp. I instead have to revert to using Func.
Author.objects.annotate(
date_edited=Func(F("articles__date_edited"), function="DATE"))
.filter(Q(articles__isnull=False) & Q(articles__date_published__date=F("date_edited"))
)
The above works. Not sure if relevant, but in settings I have USE_TZ enabled.
Change History (2)
comment:1 by , 5 years ago
| Description: | modified (diff) |
|---|
comment:2 by , 5 years ago
| Description: | modified (diff) |
|---|
Note:
See TracTickets
for help on using tickets.