/django-diffable

Primary LanguagePythonBSD 2-Clause "Simplified" LicenseBSD-2-Clause

Django Diffable

example workflow Coverage Status

About

A django abstract model that tracks model fields' values and provide some useful api to know what fields have been changed.

Install

pip install django-diffable

Usage

Inherit from DiffableModel:

from diffable.models import DiffableModel
from django.db import models


class Product(DiffableModel):
    name = models.CharField(max_length=30)

Use as follows:

>>> product = Product(name='Atari')
>>> product.has_changed
False
>>> product.changed_fields
[]
>>> product.name = 'Commodore'
>>> product.has_changed
True
>>> product.changed_fields
['name']
>>> product.diff
{'name': ('Atari', 'Commodore')}
>>> product.get_field_diff('name')
('Atari', 'Commodore')
>>> product.save()
>>> product.has_changed
False
>>> product.name = 'ZX Spectrum'
>>> product.has_changed
True
>>> product.refresh_from_db()
>>> product.has_changed
False
>>> product.name
'Commodore'

License

The Django Diffable package is licensed under the FreeBSD License.