Skip to main content
Help the Python Software Foundation raise $60,000 USD by December 31st!  Building the PSF Q4 Fundraiser

Keep a record of diffs made to a Django model or collection of models

Project description

Django Diffs

https://travis-ci.org/linuxlewis/django-diffs.svg?branch=master

Django diffs allows models to be registered to track it’s changes (or diffs) over time. It adds a manager to registered model to easily access it’s changes

It’s compatible with Python 2/3 and Django 1.8 and above. It requires a postgresql database.

How does it Work?

It allows models to be registered similar to ModelAdmin. When the model is saved the diff is serialized and stored. It relies on django-dirtyfields to track the changes on models. Changes can be accessed via the diffs manager on the registered model.

Here’s a quick example.

# models.py

import diffs

@diffs.register
class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

question = Question.objects.create(question_text='What is life?')

question.question_text = 'What is python?'
question.save()

diffs = Question.diffs.get_diffs(question.id).all()

for diff in diffs:
    print(diff.diff)

Why?

You need to track the changes over time to a single or collection of related django models.

Getting Started

  • Add django-diffs to requirements.txt
pip install django-diffs
  • Add diffs to INSTALLED_APPS
INSTALLED_APPS = (
    'diffs',
)
  • Run Migrations
python manage.py migrate
  • Register a Model
# models.py

import diffs

@diffs.register
class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

That’s it! Changes will now be tracked automatically for this model.

Custom Serialization

By default django-diffs uses django.core.serializers module to serialize the diff to json.

To use your own custom serialization format just implement the serialize_diff method on your model. It will be passed the list of dirty_fields.

# models.py

import diffs

@diffs.register
class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def serialize_diff(self, dirty_fields):
        return {'fields': dirty_fields}

question = Question.objects.create(question_text='What will happen?')

Question.diffs.get_diffs(question.id).last().diff
# {'fields': ['question_name']}

Project details


Download files

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

Files for django-diffs, version 0.0.2
Filename, size File type Python version Upload date Hashes
Filename, size django_diffs-0.0.2-py2.py3-none-any.whl (11.0 kB) File type Wheel Python version 2.7 Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page