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
|
return v
|
||||||
|
|
||||||
# updated_at can be serialized to JSON
|
# updated_at can be serialized to JSON
|
||||||
@field_serializer("updated_at")
|
@field_serializer("updated_at", "created_at")
|
||||||
def serialize_dt(self, dt: datetime, _info):
|
def serialize_datetime(value):
|
||||||
if dt is None:
|
if isinstance(value, datetime):
|
||||||
return None
|
# I'm getting 2024-05-29T17:57:17.631346
|
||||||
return dt.isoformat()
|
# 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")
|
@field_validator("updated_at", mode="before")
|
||||||
def validate_dt(cls, v):
|
def validate_dt(cls, v):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue