Keep a record of diffs made to a Django model or collection of models
Project description
Django Diffs
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for django_diffs-0.0.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e51ccd9af58e9adc279a306916ed0de16c02982e5f969712604cfe728e1724b |
|
MD5 | 84756e7185d46f1351f2c1c6b58f1864 |
|
BLAKE2b-256 | 0b8feb7fae8500b68a174a89fe8c19414603b1e66232ff1089e860f292beeedf |