mirror of
https://github.com/fastapi-practices/fastapi-best-architecture.git
synced 2026-09-21 21:15:13 +00:00
Optimize the timezone datetime return encoder (#767)
* Optimize the timezone datetime return encoder * Update the default datetime conversion
This commit is contained in:
@@ -5,7 +5,7 @@ from typing import Annotated
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, EmailStr, Field, validate_email
|
||||
|
||||
from backend.core.conf import settings
|
||||
from backend.utils.timezone import timezone
|
||||
|
||||
# 自定义验证错误信息,参考:
|
||||
# https://github.com/pydantic/pydantic-core/blob/a5cb7382643415b716b1a7a5392914e50f726528/tests/test_errors.py#L266
|
||||
@@ -124,5 +124,9 @@ class SchemaBase(BaseModel):
|
||||
|
||||
model_config = ConfigDict(
|
||||
use_enum_values=True,
|
||||
json_encoders={datetime: lambda x: x.strftime(settings.DATETIME_FORMAT)},
|
||||
json_encoders={
|
||||
datetime: lambda x: timezone.to_str(timezone.from_datetime(x))
|
||||
if x.tzinfo is not None
|
||||
else timezone.to_str(x)
|
||||
},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user