from django.db import models
class MyModel(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
- Import the
models
module from the Django package. - Create a class named
MyModel
that inherits from models.Model
. - Define a field named
created_at
of type DateTimeField
to store the creation timestamp. - Set the
auto_now_add
parameter to True
for created_at
to automatically set the field to the current timestamp when an instance of the model is created. - Define another field named
updated_at
of type DateTimeField
to store the last update timestamp. - Set the
auto_now
parameter to True
for updated_at
to automatically update the field to the current timestamp whenever the instance is saved.