Using signals in Django to log changes in models

Sometimes we need to know who made what changes to which table. This might be required for legal audit purpose or for simple organisational level logging.

There are multiple Django apps available online which can help you log the model changes but there is no fun in doing that. We will see how to do it without using ready-made app and hence will learn something in the process.

Signals:

Signals lets a sender notify another receiver that some event have occurred and some action needs to be performed.

For example, we have some data in cache as well in DB. We read data from cache and if not found then goes to DB as fallback. Now whenever a DB is updated, we need to update the cache as well. But we might update the model from multiple views. Hence it is tough and not clean to write cache update logic in every such view. Signals comes into picture now.

Continue reading “Using signals in Django to log changes in models”