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 hashes)

Uploaded Source

Built Distribution

django_field_history-0.2.0-py2.py3-none-any.whl (8.8 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