Skip to main content

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.

Table of Contents

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.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

django_diffs-0.0.2-py2.py3-none-any.whl (11.0 kB view hashes)

Uploaded Python 2 Python 3

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