Skip to main content

Track Django Model Objects over time

Project description

Model Tracker

Track model object changes over time so that you know who done what.

PyPI version

Installation

  • Install the package
pip install django-model-tracker
  • Add Application to your project's INSTALLED_APPs
INSTALLED_APPS = (
     '....',
    'ModelTracker',
    )
  • Add the following line to your urls.py
import ModelTracker
urlpatterns = patterns('',
...
url(r'^track/', include('ModelTracker.urls')),
...
)
  • Run Migrations
   python manage.py migrate ModelTracker 
  • Add the following line to your models.py file
from ModelTracker import Tracker
  • Convert each Model you want to track to inhert from Tracker.ModelTracker instead of models.Model

Old Code

   class Employee(models.Model):
     name=models.CharField(max_length=255)
     address=models.CharField(max_length=255)
     age=models.IntegerField()

New Code

 class Employee(Tracker.ModelTracker):
   name=models.CharField(max_length=255)
   address=models.CharField(max_length=255)
   age=models.IntegerField()
  • For each save() call, add the user the username
    • Old Code
   emp=Employee()
   emp.save()
 * New Code
       emp=Employee()
       emp.save(request.user.username)
  • Starting from version of 0.5, you can pass a event_name parameter to mark change as an event

    • New Code
       emp=Employee()
       emp.save(request.user.username,event_name="Created the user")

Using The Middleware

You can add ModelTracker.middleware.ModelTrackerMiddleware to your Middleware classes to get the username automatically from the request.

MIDDLEWARE_CLASSES = (
     '....',
    'ModelTracker.middleware.ModelTrackerMiddleware',
    )

Note: If you pass username as None then the change won't be saved.

Showing Record History

There are 3 ways to see the history of a record

  1. go to ModelTracker url and select Table and enter id.
  2. call showModelChanges by POST and send csrftokenmiddleware to return history as html.
  3. call getModelChanges which returns history as Json.

Django Admin

There is 2 ways to update an object by django admin

  1. Handle save and delete in ModelAdmin as follows
    def save_model(self, request, obj, form, change):
         obj.save(request.user.username,"Editing From admin interface")
    
    def delete_model(self, request, obj):
         obj.delete(username=request.user.username, event_name="Deleting From admin interface")
    
  2. Inhert from TrackerAdmin rather ModelAdmin
    from ModelTracker.Tracker import TrackerAdmin 
    admin.site.register(employee, TrackerAdmin)
    

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-model-tracker-1.7.1.tar.gz (61.0 kB view details)

Uploaded Source

File details

Details for the file django-model-tracker-1.7.1.tar.gz.

File metadata

  • Download URL: django-model-tracker-1.7.1.tar.gz
  • Upload date:
  • Size: 61.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for django-model-tracker-1.7.1.tar.gz
Algorithm Hash digest
SHA256 7e8b6293957dc0df063a45d70560f95f25d077df7ac743e22e628d83ffd7fa48
MD5 bbbd2e64a388a57d81691d6fa73b7c34
BLAKE2b-256 cf4d3fdc20122ff53c8cb870b89458bef071f81eba962355da54da6e3ae63163

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page