I am using pydantic v2 and have a custom schema for a FutureAwareDatetime.
class FutureAwareDatetime:
"""A datetime that requires timezone info and must be in the future."""
@classmethod
def __get_pydantic_core_schema__(
cls,
source: type[Any],
handler: GetCoreSchemaHandler,
) -> CoreSchema:
if cls is source:
# used directly as a type
return datetime_schema(
now_op="future",
tz_constraint="aware",
)
else:
schema = handler(source)
_check_annotated_type(schema["type"], "datetime", cls.__name__)
schema["now_op"] = "future"
schema["tz_constraint"] = "aware"
return schema
def __repr__(self) -> str:
return "FutureAwareDatetime"
While this is working fine, I want to be able to use get dt.isoformat(timespec="seconds")
when I serialize the data with e.g. model_dump()
I do not want to use the field_serializer
decorator but use it directly inside the class.
Thank you.