How to create a time Field for a model like django DateTimeField
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the SQLModel documentation, with the integrated search.
- I already searched in Google “How to X in SQLModel” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to SQLModel but to Pydantic.
- I already checked if it is not related to SQLModel but to SQLAlchemy.
Commit to Help
- I commit to help with one of those options 👆
Example Code
from datetime import datetime
from typing import Optional
from sqlmodel import Field, SQLModel, DateTime
class Image_Save_Record(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
camera_ip: str
camera_channel: int
file_path: str
file_name: str
create_time: DateTime
update_time: DateTime
Description
when i create a Image_Save_Record instance and commit it to db, the create_time field value should be that system time current.
# django version
create_time = models.DateTimeField(auto_now_add=True)
when i change the Image_Save_Record instance which i created before and commit it to db, the update_time field value should be that system time current.
# django version
update_time = models.DateTimeField(auto_now=True)
Is there sqlmodel have option to do the same like this?
Operating System
Windows
Operating System Details
No response
SQLModel Version
0.0.6
Python Version
3.7.9
Additional Context
No response
Issue Analytics
- State:
- Created a year ago
- Comments:11 (6 by maintainers)
Top Results From Across the Web
DateTimeField - Django Models - GeeksforGeeks
DateTimeField – Django Models ... DateTimeField is a date and time field which stores date, represented in Python by a datetime.datetime instance.
Read more >Creating a Django DateTimeField - eduCBA
Create a Django DateTimeField. 1. Changes in Models.py file: As mentioned in the syntax section the Date Time field needs to be declared...
Read more >Django Tips #4 Automatic DateTime Fields
Both Django's DateTimeField and DateField have two very useful arguments for automatically managing date and time.If you want keep track on ...
Read more >Automatic DateTime Fields in Django | Django Tips#4
Both Django's DateTimeField and DateField have two very useful arguments for automatically managing date and time.
Read more >Automatic creation date for Django model form objects
You can use the auto_now and auto_now_add options for updated_at and created_at respectively. class MyModel(models.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Here is my very rough example code:
Running it outputs:
So
created_at
is constant, andupdate_at
is 5 seconds aftercreated_at
as expected given the 5 second sleep between creation and update.I also implemented
updated_at
field here with the following: Add this to the yourSQLModel
:Alembic migration:
Or in case you are using batch operation: