首页 > 其他分享 >Django处理带T、带Z的时间格式

Django处理带T、带Z的时间格式

时间:2023-01-29 14:22:36浏览次数:56  
标签:USE TZ 处理 Django timezone start time tzinfo 格式

问题:

    USE_TZ = False后,django orm  查询时间报错:MySQL backend does not support timezone-aware datetimes when USE_TZ is False.

解决: 查询时间前,做替换

import pytz
from django.utils import timezone
 
 
USE_TZ = False
if USE_TZ is True:
    tzinfo_ = pytz.utc
else:
    tzinfo_ = None
 
start_time_str = '2019-11-01T16:00:00.000Z'
end_time_str = '2019-11-02T16:00:00.000Z'
# 时间做时区转换
start_time = timezone.datetime.strptime(start_time_str, '%Y-%m-%dT%H:%M:%S.%fZ').replace(tzinfo=tzinfo_)
end_time = timezone.datetime.strptime(end_time_str, '%Y-%m-%dT%H:%M:%S.%fZ').replace( tzinfo=tzinfo_)
# 查询mysql数据库
query_set = TestModel.objects.filter(create_time__range=(start_time, end_time))

  但出来后的时间会定位到16:00,少8小时

标签:USE,TZ,处理,Django,timezone,start,time,tzinfo,格式
From: https://www.cnblogs.com/mentu21/p/17072576.html

相关文章