I want to implement a row version number that increments whenever a table row is updated.
I'm using PostgreSQL.
Essentially I want this field to behave similarly to the following updated_at
timestamp field:
updated_at = models.DateTimeField(auto_now=True)
Except instead of updating with the current time, I want to auto-increment the current field value of the row. So if the row had version=1
and I made an update on the row, it would autoincrement that row to version=2
.
I know I can implement this by overwriting the model save()
method, but I was wondering if there was a way to implement this on the database level (and ideally through the Django ORM) so that it applies to any database accessing script/query.