Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing a per-model table modification time in Django?

Tags:

python

sql

django

I have a Django application which edits a database table, which another application polls and uses to update a downstream system. In order to minimize processing when the database has not been altered in between polls, I would like to use a global modification time for a model, which is updated every time a row is created/deleted/modified. How can I do this within the Django ORM?

like image 904
kdt Avatar asked Nov 05 '22 10:11

kdt


1 Answers

Django does not give you access, nor does it maintain, a "last modified" date on a table (model). You need to implement this by yourself, but this isn't complicated.

The easiest way would be to catch the necessary signals in your model by implementing the post_save() and post_delete() model signals (hooks, basically), and maintaining a static date field which represents the "last modified" date you are looking for.

like image 149
Yuval Adam Avatar answered Nov 10 '22 01:11

Yuval Adam