-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Converting date objects #51
Comments
Hi @mudassarzahid, it turns out this actually is possible. I didn't expect that! All you need to do is add Example: import datetime
from pydantic import BaseModel, Field
class MyModel(BaseModel):
a: datetime.date = Field(json_schema_extra={"tsType": "Date"})
b: datetime.datetime = Field(json_schema_extra={"tsType": "Date"}) yields export interface MyModel {
a: Date;
b: Date;
} |
And if you want to automatically make this change EVERYWHERE in a project, I'd recommend using import datetime
from typing import Annotated
from pydantic import BaseModel, Field
Date = Annotated[datetime.date, Field(json_schema_extra={"tsType": "Date"})]
DateTime = Annotated[datetime.datetime, Field(json_schema_extra={"tsType": "Date"})]
class MyModel(BaseModel):
a: Date
b: DateTime Use the annotated You could probably also do this by modifying the pydantic schema generator, but those APIs always seem to be in flux so in my eyes it's not worth the effort. |
Does this work as a solution for you? I don't think I'll make this into a standard thing, since |
@phillipdupuis That's perfect, thank you! |
Is there a way to support
datetime
/date
toDate
conversions? Currently, they are converted tostring
.The text was updated successfully, but these errors were encountered: