Joseph
Joseph

Reputation: 13

How to automatically handle creation and update date on a Django model?

I'm trying to have an immutable field(created_at) and be able to update it(updated_at) without altering the original created_at field. This is a django project if that matters

Upvotes: 1

Views: 614

Answers (1)

Pierre V.
Pierre V.

Reputation: 1625

Use auto_now and auto_now_add (see documentation).

class Book(models.Model):
    ...
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

Upvotes: 2

Related Questions