24π
@Simon Wilder perfectly answer why itβs not working, here is how you can actually solve it without disabling TZ support in django
Django document give instruction to install time zone definition to database:
SQLite: install
pytz
β conversions are actually performed in Python.PostgreSQL: no requirements (see Time Zones).
Oracle: no requirements (see Choosing a Time Zone File).
MySQL: install
pytz
and load the time zone tables withmysql_tzinfo_to_sql
.
In my case : mysql and Mac Os, following command solve the problem:
sudo mysql_tzinfo_to_sql /usr/share/zoneinfo/ | mysql -u root mysql
26π
I was seeing exactly the same behaviour as you.
If you check the documentation for 1.6 and the month queryset. They have added the following paragraph:
βWhen USE_TZ is True, datetime fields are converted to the current time zone before filtering. This requires time zone definitions in the database.β
If you change the following line in your settings to False, then you should start getting the data back that youβre expecting.
USE_TZ = False
- [Django]-Add Python Application to accept incoming network connections
- [Django]-Implementing multiple user types with Django 1.5
- [Django]-Having trouble with user.is_authenticated in django template
2π
Your syntax is incorrect. It should be:
Clicks.objects.filter(created__month=2)
(you left off the βobjectsβ manager)
- [Django]-How to save output from django call_command to a variable or file
- [Django]-Using a Django custom model method property in order_by()
- [Django]-Adding a user to a group in django
0π
To update the answer here since I ran into the above issue but none of the solutions worked. Most new mysql installations come pre-installed with tz-info, so the mysql_tzinfo_to_sql command wont really help. And setting TZ_INFO to False isnβt really a solution since many need time-zone aware datetime.
So, what worked for me was to create a tz aware datetime object and check against that. Lets say you wanna filter records for today you would do something like,
from datetime import datetime
import pytz
today = datetime.now().replace(tzinfo=pytz.UTC).date() # tz aware datetime object
todays_records = myModel.objects.filter(created__year=today.year, created__month=today.month,created__day=today.day)
Hope this helps.
- [Django]-What's the best option to process credit card payments in Django?
- [Django]-Using mock to patch a celery task in Django unit tests
- [Django]-Django: Search form in Class Based ListView
0π
Solution for windows, 2022 β how to make django with USE_TZ=True
be able to filter by month and day, not just year. Example of now working code: queryset.filter(created__month=1)
- download the timezone file from here: https://dev.mysql.com/downloads/timezones.html
- make sure you get the right one β for mysql8 I used the file for 5.7
- unzip it β see that itβs just a
sql
file - load it into your mysql database. something like
mysql -u root -p mysql < FILEPATH
- restart mysql service (
control panel
|administration tools
|services
| find mysql |restart
) - validation:
mysql
|use mysql
|select * from time_zone
- you should see a few thousand entries. If not, it means you didnβt load the sql file in correctly.
- rerun django methods using filtering and it should work.
Note: this is different than varnothing
βs answer above because on windows, dealing with filetypes or things like mysql_tzinfo_to_sql
may be different due to pathing. This one works because you fall back to clear actions like "load this sql file into this db"
- [Django]-Django β how to make ImageField/FileField optional?
- [Django]-Django admin sort foreign key field list
- [Django]-Change list display link in django admin