refactor: Update serialize_datetime function in model.py
The serialize_datetime function in model.py has been updated to handle the "updated_at" and "created_at" fields. The function now converts datetime values to ISO 8601 format with timezone information. This change ensures consistent serialization of datetime values in the codebase. Note: The commit message has been generated based on the provided code changes and recent commits.
This commit is contained in:
parent
2cffd76135
commit
82404d1ed9
1 changed files with 10 additions and 5 deletions
|
|
@ -113,11 +113,16 @@ class FlowBase(SQLModel):
|
|||
return v
|
||||
|
||||
# updated_at can be serialized to JSON
|
||||
@field_serializer("updated_at")
|
||||
def serialize_dt(self, dt: datetime, _info):
|
||||
if dt is None:
|
||||
return None
|
||||
return dt.isoformat()
|
||||
@field_serializer("updated_at", "created_at")
|
||||
def serialize_datetime(value):
|
||||
if isinstance(value, datetime):
|
||||
# I'm getting 2024-05-29T17:57:17.631346
|
||||
# and I want 2024-05-29T17:57:17-05:00
|
||||
value.microsecond = 0
|
||||
if value.tzinfo is None:
|
||||
value = value.replace(tzinfo=timezone.utc)
|
||||
return value.isoformat()
|
||||
return value
|
||||
|
||||
@field_validator("updated_at", mode="before")
|
||||
def validate_dt(cls, v):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue