DEV Community

Cover image for Tracking Database Changes in Django
Mohamed M El-Kalioby
Mohamed M El-Kalioby

Posted on

Tracking Database Changes in Django

First, the credit of the cover photo is for

When working in mission-critical applications, it is critical to know when and how an object (DB row) got here.

For that, we created django-model-tracker a Django application that handle tracking Django Models changes with the username of the user who did the change, then the admin team can view the changes and restore the object to an older state.


  1. Install the package from pip pip install django-model-tracker
  2. In the add ModelTracker to INSTALLED_APPS.

  3. Run the migrations for the application
    python migrate ModelTracker

Tracking your models

To track a model, you need to change the inheritance from models.Model to ModelTracker.Tracker e.g

from ModelTracker import Tracker
class Employee(Tracker.ModelTracker):
Enter fullscreen mode Exit fullscreen mode

When you save an Employee object, you can pass the username as follows

Enter fullscreen mode Exit fullscreen mode


  1. If you don't pass a username, ModelTracker will check pick the username from a ModelTracker middleware (ModelTracker.middleware.ModelTrackerMiddleware)
  2. You can pass username as None to skip saving the tracking info.
  3. The username is a string so you can give names for the management commands as you like.
  4. There is second optional parameter which event_name which can be used a tag the change.

This is a quick tour for django-model-tracker, you can find more information on the GitHub Repo

Top comments (0)