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:
ogabrielluiz 2024-05-31 11:22:40 -03:00
commit 82404d1ed9

View file

@ -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):