django model current timestamp

from django.db import models

class MyModel(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
  1. Import the models module from the Django package.
  2. Create a class named MyModel that inherits from models.Model.
  3. Define a field named created_at of type DateTimeField to store the creation timestamp.
  4. 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.
  5. Define another field named updated_at of type DateTimeField to store the last update timestamp.
  6. Set the auto_now parameter to True for updated_at to automatically update the field to the current timestamp whenever the instance is saved.