Skip to main content

A Django app to track changes to a model field.

Project description

https://badge.fury.io/py/django-field-history.svg Documentation Status https://travis-ci.org/grantmcconnaughey/django-field-history.svg?branch=master https://coveralls.io/repos/github/grantmcconnaughey/django-field-history/badge.svg?branch=master

A Django app to track changes to a model field.

Documentation

The full documentation is at https://django-field-history.readthedocs.org.

Quickstart

Install django-field-history:

pip install django-field-history

Be sure to put it in INSTALLED_APPS.

INSTALLED_APPS = [
    'field_history',
]

Then add it to your models.

from field_history.tracker import FieldHistoryTracker

class PizzaOrder(models.Model):
    INVOICE_NUMBER_TYPE = (
        ('ORDER', 'Ordered'),
        ('COOK', 'Cooking'),
        ('COMPL', 'Complete'),
    )
    status = models.CharField(max_length=10, choices=PIZZA_CHOICES)

    field_history = FieldHistoryTracker(['status'])

Now each time you change the order’s status field information about that change will be stored in the database.

from field_history.models import FieldHistory

# No FieldHistory objects yet
assert FieldHistory.objects.count() == 0

# Creating an object will make one
pizza_order = PizzaOrder.objects.create(status='ORDER')
assert FieldHistory.objects.count() == 1

# This object has some fields on it
history = FieldHistory.objects.get()
assert history.object == pizza_order
assert history.field_name == 'status'
assert history.field_value == 'ORDER'
assert history.date_created is not None

# Updating that particular field creates a new FieldHistory
pizza_order.status = 'COOK'
pizza_order.save()
assert FieldHistory.objects.count() == 2

# You can query FieldHistory objects this way
histories = FieldHistory.objects.get_for_model_and_field(pizza_order, 'status')
assert list(pizza_order.field_history) == list(histories)

# Or using the get_{field_name}_history() method added to your model
self.assertItemsEqual([pizza_order.get_status_history()], [histories])

updated_history = histories.order_by('-date_created').first()
assert history.object == pizza_order
assert history.field_name == 'status'
assert history.field_value == 'COOK'
assert history.date_created is not None

Features

  • Keeps a history of all changes to a particular field.

  • Stores the field’s name, value, date and time of change.

  • Works with all model field types (except ManyToManyField).

Running Tests

Does the code actually work?

source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install -r requirements-test.txt
(myenv) $ python runtests.py

History

0.2.0 (2016-02-17)

  • First release on PyPI.

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-field-history-0.2.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

django_field_history-0.2.0-py2.py3-none-any.whl (8.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-field-history-0.2.0.tar.gz.

File metadata

File hashes

Hashes for django-field-history-0.2.0.tar.gz
Algorithm Hash digest
SHA256 46f5683a18e8bb7513dfed5ef7c5cddad1d3cef376096e72f63ae3cc7664dc8b
MD5 35f86f05d51ab4c94a7887e3b1c279d9
BLAKE2b-256 7057959e233e87da3025e2cead6dc8a3ca342dc2b8bbdac21c8afeaeb2ef0666

See more details on using hashes here.

File details

Details for the file django_field_history-0.2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_field_history-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7c4392d1e55a54cb2a2f894e717704f7e530bf7a7de765272e5096fbc0dfb443
MD5 c389dfec53afca886a5c93580873ac9d
BLAKE2b-256 962af7873f7168830a9bfdcad91191dbb49c128731412a3efc6073930479fb7d

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