Hi,
I introduced a custom date class
CustomDate(datetime.date):
pass
and found, that it isn’t recognized as date
when I filter
MyModel.objects.filter(some_date_field=CustomDate.today())
Is this intentionally or is it a bug?
Hi,
I introduced a custom date class
CustomDate(datetime.date):
pass
and found, that it isn’t recognized as date
when I filter
MyModel.objects.filter(some_date_field=CustomDate.today())
Is this intentionally or is it a bug?
The date
“object” in Python isn’t a standard class.
From inside a Python shell:
In [8]: import datetime
In [9]: type(datetime)
Out[9]: module
In [10]: type(datetime.datetime)
Out[10]: type
In [11]: type(datetime.datetime.date)
Out[11]: method_descriptor
In [13]: class CustomDatetime(datetime.datetime):
...: pass
...:
In [14]: CustomDatetime.today()
Out[14]: CustomDatetime(2024, 6, 27, 11, 56, 21, 678109)
In [15]: type(CustomDatetime.today())
Out[15]: __main__.CustomDatetime
In [16]: User.objects.filter(last_login=CustomDatetime.today())
/home/tskww/ve/d5-c4/lib/python3.12/site-packages/django/db/models/fields/__init__.py:1659: RuntimeWarning: DateTimeField User.last_login received a naive datetime (2024-06-27 11:57:30.531250) while time zone support is active.
warnings.warn(
Out[16]: <QuerySet []>
In [17]: class CustomDate(datetime.datetime.date):
...: pass
...:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[17], line 1
----> 1 class CustomDate(datetime.datetime.date):
2 pass
TypeError: cannot create 'method_descriptor' instances
In [18]: from datetime import datetime
In [19]: class CustomDate(datetime.date):
...: pass
...:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[19], line 1
----> 1 class CustomDate(datetime.date):
2 pass
TypeError: cannot create 'method_descriptor' instances
In [20]: class CustomDate(datetime):
...: pass
...:
In [21]: User.objects.filter(last_login=CustomDate.today())
/home/tskww/ve/d5-c4/lib/python3.12/site-packages/django/db/models/fields/__init__.py:1659: RuntimeWarning: DateTimeField User.last_login received a naive datetime (2024-06-27 12:00:06.348283) while time zone support is active.
warnings.warn(
Out[21]: <QuerySet []>
Second session, after logging in to the website:
In [1]: from datetime import datetime
In [2]: class CustomDate(datetime):
...: pass
...:
In [3]: User.objects.filter(last_login__date=CustomDate.today().date())
Out[3]: <QuerySet [<User: tskww>]>
Hi @KenWhitesell thanks for the quick response and I’m very sorry for wasting your time …
I actually did use the date type (and not the date method of the datetime class) …
In my more complex code this didn’t work … but in the simple test case I wrote now, it works as expected …
I may need to dig deeper to understand what’s wrong.
Ahh … I found the issue. My “simple” test case was too simple.
My CustomDate class looks actually like this:
from datetime import date
class CustomDate(date):
def __str__(self) -> str:
return self.strftime("%Y%m%d")
This is a bit surprising … but hey … a lot of magic happens in django