fix(merchant): 操作人统计时间筛选兼容 USE_TZ=False

MySQL 在关闭 USE_TZ 时不接受 timezone-aware datetime,解析为 naive 再查询。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
XingQue
2026-07-15 23:53:52 +08:00
parent 9c029288ba
commit 477129de46

View File

@@ -15,23 +15,36 @@ from products.models import ShangpinLeixing
def _parse_dt(value, end_of_day=False):
"""解析时间USE_TZ=False 时必须返回 naive datetimeMySQL"""
if value is None or value == '':
return None
dt = None
if isinstance(value, datetime):
return value
dt = value
else:
text = str(value).strip().replace('/', '-')
for fmt in ('%Y-%m-%d %H:%M:%S', '%Y-%m-%d %H:%M', '%Y-%m-%d'):
try:
dt = datetime.strptime(text, fmt)
if fmt == '%Y-%m-%d' and end_of_day:
dt = dt.replace(hour=23, minute=59, second=59)
if timezone.is_naive(dt):
dt = timezone.make_aware(dt, timezone.get_current_timezone())
return dt
break
except ValueError:
continue
if dt is None:
return None
from django.conf import settings
use_tz = bool(getattr(settings, 'USE_TZ', False))
if use_tz:
if timezone.is_naive(dt):
dt = timezone.make_aware(dt, timezone.get_current_timezone())
else:
if timezone.is_aware(dt):
dt = timezone.make_naive(dt, timezone.get_current_timezone())
return dt
def _money(val):
if val is None: