Opened 4 years ago

Last modified 4 years ago

#31860 closed Bug

"__date" in F expression not working, have to use Func() — at Initial Version

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

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 (0)

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