#34699: Filtering on annotated TruncSecond expression gives unexpected result.
-------------------------------------+-------------------------------------
     Reporter:  Stefan               |                    Owner:  Francesco
         Type:                       |  Panico
  Cleanup/optimization               |                   Status:  assigned
    Component:  Database layer       |                  Version:  4.2
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

 * type:  Uncategorized => Cleanup/optimization
 * stage:  Unreviewed => Accepted


Comment:

 I did some more research on this. I'm accepting this ticket because I do
 believe there is an unexpected behavior, which may be a bug or just a
 documentation issue.

 The simplest test of getting all books and printing their `published` date
 and their truncated-to-seconds published date is, IMHO, unexpected: the
 `published` value is in UTC but the annotated value is in the timezone the
 app is configured. My settings:

 {{{
 USE_TZ = True
 TIME_ZONE = "Europe/Berlin"
 }}}

 The model:

 {{{
 from django.db import models
 from django.utils import timezone


 class Book(models.Model):
     published = models.DateTimeField(default=timezone.now)
 }}}

 In the shell:
 {{{
 import time
 from django.db.models.functions import TruncSecond
 from testapp.models import Book
 for i in range(3):
     Book.objects.create(); time.sleep(1)
 annotated_books =
 Book.objects.annotate(_published_trunc=TruncSecond('published'))
 for i in annotated_books.all():
     print(i.published, i._published_trunc)
 }}}

 With output:
 {{{
 2023-07-14 18:39:22.620603+00:00 2023-07-14 20:39:22+02:00
 2023-07-14 18:39:28.585856+00:00 2023-07-14 20:39:28+02:00
 2023-07-14 18:39:29.590469+00:00 2023-07-14 20:39:29+02:00
 2023-07-14 18:39:30.595811+00:00 2023-07-14 20:39:30+02:00
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34699#comment:6>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018955c43298-5f8bdd89-8b47-4bff-bf14-54dea8795a3a-000000%40eu-central-1.amazonses.com.

Reply via email to