Last modified: Jan 10, 2023 By Alexander Williams

Django: How to Create The Last Modified Field

The last Modified Field is a date/time field that automatically updates to the current date/time when the model's data is modified.

Create The last Modified Field

Let's create a Topics model that contains title, content, date_modified, and the date_published field.


class Topics(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    date_modified = models.DateTimeField(auto_now=True)
    date_published = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

auto_now: Updating on creation and modifation.
auto_now_add: Updating on creation only.



Now, let's test our model.

Creating a new topic:

Django: How to Create The Last Modified Field

Updating the topic:

Django: How to Create The Last Modified Field

As you can see, date_modified has been updated.