You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was testing FastAPI and came to odmantic as I am using mongoDB. I have a question regarding creating a model. How can I define a default value for created_date and updated_date? Is there any equivalent to Django'sDateField.auto_now_addandDateField.auto_now`
Solution
Something like Django have I guess if possible.
Alternative solutions
Using default_factory for created_date (I haven't tested it yet btw) and none for modified_date
Additional context
None.
Regards
The text was updated successfully, but these errors were encountered:
I just stumbled upon this question because I was having the same creation date in every object in my application:
So, if you use default to set the value, then you will always have the same value for every object instance: creation_date: datetime = Field(default=datetime.now(), alias="creation_date", description="Task creation date")
To get the current date/datetime every time, you need to use default_factory with the datetime.now or datetime.date.today method call: creation_date: datetime = Field(default_factory=datetime.now, description="Creation date")
Feature request
I just discovered this I really love it ❤️
Context
I was testing FastAPI and came to odmantic as I am using mongoDB. I have a question regarding creating a model. How can I define a default value for
created_date
andupdated_date
? Is there any equivalent to Django'sDateField.auto_now_addand
DateField.auto_now`Solution
Something like Django have I guess if possible.
Alternative solutions
Using
default_factory
forcreated_date
(I haven't tested it yet btw) and none formodified_date
Additional context
None.
Regards
The text was updated successfully, but these errors were encountered: