Skip to content

Commit b9d506f

Browse files
committed
Optimize the timezone datetime return encoder
1 parent bd804e0 commit b9d506f

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

backend/common/schema.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pydantic import BaseModel, ConfigDict, EmailStr, Field, validate_email
77

88
from backend.core.conf import settings
9+
from backend.utils.timezone import timezone
910

1011
# 自定义验证错误信息,参考:
1112
# https://github.com/pydantic/pydantic-core/blob/a5cb7382643415b716b1a7a5392914e50f726528/tests/test_errors.py#L266
@@ -124,5 +125,9 @@ class SchemaBase(BaseModel):
124125

125126
model_config = ConfigDict(
126127
use_enum_values=True,
127-
json_encoders={datetime: lambda x: x.strftime(settings.DATETIME_FORMAT)},
128+
json_encoders={
129+
datetime: lambda x: timezone.to_str(timezone.from_datetime(x))
130+
if x.tzinfo is not None
131+
else x.strftime(settings.DATETIME_FORMAT)
132+
},
128133
)

0 commit comments

Comments
 (0)