In Django ORM objects.filter () can not filter for filtering the month

filter date django query attributes: year, month, day, week_day, hour, minute, second

In doing project review blog, I moved to linux project from the windows, and then blog articles archive that piece, filtered according to the results of the month is always empty. Then Baidu a bit, the result is a problem area.

In the configuration file settings.py Django, there are now two configuration parameters related to time and time zone, and USE_TZ are TIME_ZONE

In the default setting Django TIME_ZONE time zone America / Chicago

If TIME_ZONE set to another time zone, then the situation also points:

  • If it is a Windows system, the TIME_ZONE setting is useless, Django will use the machine time
  • If the other system, the time in the zone of use, such as setting USE_TZ = False, TIME_ZONE = 'Asia / Shanghai', Shanghai using UTC time.

When the corresponding django query the database ORM statement by using the mysql comes time to deal with the time zone feature USE_TZ = True enable functions such as convert_tz (time transfer function), we set in the setting file django project, district TIME_ZONE setting = 'Asia / Shanghai ', but do not know mysql Shanghai this time zone, so mysql can not get the correct time Asia / Shanghai, and when the query returns an empty list.

First on the ultimate solution:

# 可以使用如下方式进行筛选,筛选的值格式和你DB中的格式对应,比如是2019/04/12还是2017-04-12
article_list = ArticleModel.objects.filter(status='p', add_time__startswith='2019-04-12').order_by('-add_time')

#如果只想根据年月来筛选就是
article_list = ArticleModel.objects.filter(status='p', add_time__startswith='20179-04').order_by('-add_time')

Note that month and less than 10 months to match the database need to add a 0, namely in May 2019 from the need to '2019-5' becomes '2019-05'

Solution 1:

Directly in setting.py file: USE_TZ = False

But then you can not use django of the time zone feature. (Stored in the database will be little time to 8 hours)

Solution 2:

mysql official documentation written two systems naturally do not contain time zone conversion of this file, it does not support windows time zone conversion, as shown:

So if in Linx, directly execute this command, as shown:

If you are in windows:
When you download the file mysql zone: https: //dev.mysql.com/downloads/timezones.html

 Copy to C: \ \ \ MySQL Server \ Data mysql directory ProgramData MySQL 5.7 \
       restart mysql
       log mysql, execute:
        the SET TIME_ZONE the GLOBAL = 'Asia / of Shanghai';
        the SET TIME_ZONE the SESSION = 'Asia / of Shanghai';
        the SELECT @@ Global. time_zone, @@ session.time_zone;

Source: https: //blog.csdn.net/gmsGms_gms/article/details/80748208

About django time zone:

django documentation in a note: When using the time zone, all date and time information Django stored in the database are in a time zone UTC prevail in the background sometimes datetime zone, when the front desk users, on a web page translated into the user's time zone is located .

Namely:
Django If you open the Time Zone function that allows internal Django all the time as UTC time (Beijing time is UTC + 8) treatment. All storage (e.g., stored in the database mysql) and internal processing (processing various times in the background), and even a direct print in the form of a display are all UTC.

This time, when the template input time, time will enter into UTC time, when the template output time, back to the UTC time (UTC background all the time) converted into time user location (unless the application support where users set their own time zone, usually we do not care template time zone problem. when show time template will be used to set settings.TIME_ZONE are automatically converted into the UTC time time rendering time zone settings.TIME_ZONE lies.)

After enabling USE_TZ = True, at the time of processing time to ensure that the following two points:

  • Stored in the database to ensure that the UTC time;
  • When time parameters passed between functions (background processing) time to ensure it has been converted into UTC time;

For example, usually get the current time using a

import datetime
now = datetime.datetime.now()

After enabling USE_TZ = True, need to be written

import datetime 
from django.utils.timezone import utc
utcnow = datetime.datetime.utcnow().replace(tzinfo=utc)

转自:https://www.cnblogs.com/ChangAn223/p/10928230.html

filter date django query attributes: year, month, day, week_day, hour, minute, second

In doing project review blog, I moved to linux project from the windows, and then blog articles archive that piece, filtered according to the results of the month is always empty. Then Baidu a bit, the result is a problem area.

In the configuration file settings.py Django, there are now two configuration parameters related to time and time zone, and USE_TZ are TIME_ZONE

In the default setting Django TIME_ZONE time zone America / Chicago

If TIME_ZONE set to another time zone, then the situation also points:

  • If it is a Windows system, the TIME_ZONE setting is useless, Django will use the machine time
  • If the other system, the time in the zone of use, such as setting USE_TZ = False, TIME_ZONE = 'Asia / Shanghai', Shanghai using UTC time.

When the corresponding django query the database ORM statement by using the mysql comes time to deal with the time zone feature USE_TZ = True enable functions such as convert_tz (time transfer function), we set in the setting file django project, district TIME_ZONE setting = 'Asia / Shanghai ', but do not know mysql Shanghai this time zone, so mysql can not get the correct time Asia / Shanghai, and when the query returns an empty list.

First on the ultimate solution:

# 可以使用如下方式进行筛选,筛选的值格式和你DB中的格式对应,比如是2019/04/12还是2017-04-12
article_list = ArticleModel.objects.filter(status='p', add_time__startswith='2019-04-12').order_by('-add_time')

#如果只想根据年月来筛选就是
article_list = ArticleModel.objects.filter(status='p', add_time__startswith='20179-04').order_by('-add_time')

Note that month and less than 10 months to match the database need to add a 0, namely in May 2019 from the need to '2019-5' becomes '2019-05'

Solution 1:

Directly in setting.py file: USE_TZ = False

But then you can not use django of the time zone feature. (Stored in the database will be little time to 8 hours)

Solution 2:

mysql official documentation written two systems naturally do not contain time zone conversion of this file, it does not support windows time zone conversion, as shown:

So if in Linx, directly execute this command, as shown:

If you are in windows:
When you download the file mysql zone: https: //dev.mysql.com/downloads/timezones.html

 Copy to C: \ \ \ MySQL Server \ Data mysql directory ProgramData MySQL 5.7 \
       restart mysql
       log mysql, execute:
        the SET TIME_ZONE the GLOBAL = 'Asia / of Shanghai';
        the SET TIME_ZONE the SESSION = 'Asia / of Shanghai';
        the SELECT @@ Global. time_zone, @@ session.time_zone;

来源:https://blog.csdn.net/gmsGms_gms/article/details/80748208

关于django中的时区:

django文档中有说明:当使用时区时,Django存储在数据库中的所有日期时间信息都以UTC时区为准,在后台使用有时区的datetime,前台用户使用时,在网页上翻译成用户所在的时区。

即:
Django 如果开启了 Time Zone 功能,即让 Django 内部把时间全部当成 UTC 时间(北京时间为 UTC+8 )对待。所有的存储(如存储到mysql数据库)和内部处理(在后台的各种时间处理),甚至包括直接 print 显示全都是 UTC 形式的。

这个时候,在通过模板输入时间时,会把输入的时间转换成UTC的时间,在通过模板输出时间的时候,回把UTC时间(后台都是UTC时间)转换成用户所在地的时间(除非应用支持用户设置自己所在的时区,通常我们不需要关心模板的时区问题。模板在展示时间的时候,会使用 settings.TIME_ZONE 中的设置自动把 UTC 时间转成 settings.TIME_ZONE 所在时区的时间渲染。)

启用 USE_TZ = True 后,在处理时间的时候要保证下面两点:

  • 保证存储到数据库中的是 UTC 时间;
  • 在函数之间传递时间参数时(后台处理)确保时间已经转换成 UTC 时间;

比如,通常获取当前时间用的是

import datetime
now = datetime.datetime.now()

启用 USE_TZ = True 后,需要写成

import datetime 
from django.utils.timezone import utc
utcnow = datetime.datetime.utcnow().replace(tzinfo=utc)

转自:https://www.cnblogs.com/ChangAn223/p/10928230.html

Guess you like

Origin www.cnblogs.com/aizhinong/p/12313054.html