Artur Ataíde
Artur Ataíde

Reputation: 139

Filter django query by day

I'm filtering a query by day of month. The problem is that in my local machine everything is doing ok and i'm getting what is expected, but in production server the query set returns None.

Local

>>> Works.objects.filter(work_estimated_start_time__day=06)
>>> [<Works: 61: ns>]
>>> w[0].work_estimated_start_time
>>> datetime.datetime(2015, 12, 6, 15, 38, 37, tzinfo=<UTC>)

Server

>>> Works.objects.filter(work_estimated_start_time__day=06)
>>> []
>>> w = Works.objects.all()
>>> w[0].work_estimated_start_time
>>> datetime.datetime(2015, 12, 6, 15, 38, 2, tzinfo=<UTC>)

The database data are pretty much the same.

Query from both environments (both are identical):

SELECT api_works.`work_id`, `api_works`.`work_central_id_id`, `api_works`.`work_user_id_id`, `api_works`.`work_driver_id_id`, `api_works`.`work_truck_id_id`, `api_works`.`work_estimated_start_time`, `api_works`.`work_start_time`, `api_works`.`work_end_time`, `api_works`.`work_first_pause_start_time`, `api_works`.`work_first_pause_end_time`, `api_works`.`work_second_pause_start_time`, `api_works`.`work_second_pause_end_time`, `api_works`.`work_creation_date`, `api_works`.`work_status`, `api_works`.`work_done`, `api_works`.`work_cancelation`, `api_works`.`work_enterprise_id_id`, `api_works`.`work_truck_type`
FROM `api_works`
WHERE EXTRACT(DAY FROM CONVERT_TZ(`api_works`.`work_estimated_start_time`, 'UTC', Europe/Zurich)) = 6

Anyone can help me? Thanks

Upvotes: 2

Views: 1210

Answers (1)

Tomasz Jakub Rup
Tomasz Jakub Rup

Reputation: 10680

MySQL don't have native TimeZone operations in Django.

When USE_TZ is True, datetime fields are converted to the current time zone before filtering. To do it, You must have installed pytz and load the time zone tables with mysql_tzinfo_to_sql.

Your production DB don't have mysql_tzinfo_to_sql loaded.

Solution 1

Load mysql_tzinfo_to_sql on production

Solution 2

Set USE_TZ to False

Solution 3

Change DB to PostgreSQL. TZ support is out-of-the-box.

Look at example MySQL without TZ support.

Upvotes: 1

Related Questions