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 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”

Creating custom user model and custom authentication in Django

While working on some django project you might feel that the default user model is not fulfilling all the requirements. For example you may want to authenticate users by email Id and not by username. You may want to store some more extra information in the user model. In short you might want to define your own custom user model.

In particular we may encounter one out of below four scenarios:

  • You are happy with the way default authentication but you need to store extra information in the model itself.
  • You want different authentication process, for example using email, but do not want to store extra information.

Continue reading “Creating custom user model and custom authentication in Django”