DEV Community

Cover image for Make your Django models self-timestamp
Hossein Kazemi
Hossein Kazemi

Posted on

Make your Django models self-timestamp

Make your Django models inherit from the below abstract class. You models will get automatically timestamped upon creation and update of instances.

from django.db import models


class TimeStampedModel(models.Model):
    """
    An abstract base class model that provides self-updating
    ``created_at`` and ``updated_at`` fields.
    """
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True
Enter fullscreen mode Exit fullscreen mode

Join my free newsletter BORING BACKEND. Soon i'll be starting mentoring people by doing micro-projects.

Top comments (0)