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