I thought that it would a good idea to point this because it is a bug that 
you will not catch until production stage, but I guess time dependant 
queries are prone to this and require experience.

After reading your comments I realize this is more a python problem than a 
Django one, as both of you said.

Thank you for your feedback on this


On Sunday, 30 October 2022 at 00:38:50 UTC+2 Adam Johnson wrote:

> I'm inclined to agree with David here. This is a “understanding Python” 
> thing. We can’t feasibly add documentation warnings everywhere such a 
> mistake could be made.
>
> On Sat, Oct 29, 2022 at 8:00 PM David Sanders <shang.xia...@gmail.com> 
> wrote:
>
>> Hi Daniel,
>>
>> I don't think a warning is necessary here as it's fairly standard Python.
>>
>> To explain: the timezone.now() is evaluated at module level – ie only 
>> once when the Python module is imported – which explains the behaviour that 
>> you're experiencing.
>>
>> If you like you can submit a documentation PR with some suggestions but 
>> others may also doubt the value of adding a warning given that the very 
>> next section explains how to do dynamic filtering. Personally I think by 
>> the time you get to advanced Django such as this, Python experience is 
>> assumed :)
>>
>> Regards,
>> David
>>
>> On Sun, 30 Oct 2022 at 00:02, Daniel Gayoso González <danig...@gmail.com> 
>> wrote:
>>
>>> Hello,
>>>
>>> Following the example in 
>>> https://docs.djangoproject.com/en/4.1/topics/class-based-views/generic-display/#viewing-subsets-of-objects
>>>  
>>> I tried the following
>>>
>>> class BookListView(ListView):
>>>     model = Book
>>>     queryset = Book.objects.filter(publication_date__lte=timezone.now())
>>>
>>> I found that this code snippet to retrieve only books that has the 
>>> publication date before today (assuming that could be books with 
>>> publication date in the future) not work as I would expected. 
>>>
>>> After some digging, I found that timezone.now() it's cached when server 
>>> starts up (in a production environment), so this query filter by the date 
>>> the server was started. So the solution is to use a dynamic filtering. 
>>>
>>> Could be a good idea to include some warning about this in the 
>>> documentation?
>>>
>>> Thanks,
>>> Daniel
>>>
>>> -- 
>>> You received this message because you are subscribed to the Google 
>>> Groups "Django developers (Contributions to Django itself)" group.
>>> To unsubscribe from this group and stop receiving emails from it, send 
>>> an email to django-develop...@googlegroups.com.
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/django-developers/CAHLCT5d3Dz3KTfpxhLQAUZjruhRLgb_XKbUKdv7VgHW_-VVfCg%40mail.gmail.com
>>>  
>>> <https://groups.google.com/d/msgid/django-developers/CAHLCT5d3Dz3KTfpxhLQAUZjruhRLgb_XKbUKdv7VgHW_-VVfCg%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django developers (Contributions to Django itself)" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to django-develop...@googlegroups.com.
>>
> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-developers/CADyZw-59A5U0pLOuZ%2BxdfHp%3Dq%3Ddwgw%2BG_GFaRYQAxfdytX6%2B5Q%40mail.gmail.com
>>  
>> <https://groups.google.com/d/msgid/django-developers/CADyZw-59A5U0pLOuZ%2BxdfHp%3Dq%3Ddwgw%2BG_GFaRYQAxfdytX6%2B5Q%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/ce97c396-e563-4cbd-aa8c-23b9c8a5e481n%40googlegroups.com.
  • War... Daniel Gayoso González
    • ... David Sanders
      • ... 'Adam Johnson' via Django developers (Contributions to Django itself)
        • ... danigayo...@gmail.com

Reply via email to