Skip to main content

History tracking for Django and Postgres

Project description

django-pghistory

django-pghistory tracks changes to your Django models using Postgres triggers, providing:

  • Reliable history tracking everywhere with no changes to your application code.
  • Structured history models that mirror the fields of your models.
  • Grouping of history with additional context attached, such as the logged-in user.

django-pghistory has a number of ways in which you can configure history tracking for your application's needs and for performance and scale. An admin integration and middleware is included out of the box too.

Quick Start

Decorate your model with pghistory.track. For example:

import pghistory

@pghistory.track()
class TrackedModel(models.Model):
    int_field = models.IntegerField()
    text_field = models.TextField()

Above we've tracked TrackedModel. Copies of the model will be stored in a dynamically-created event model on every insert and update.

Run python manage.py makemigrations followed by migrate and voila, every change to TrackedModel is now stored. This includes bulk methods and even changes that happen in raw SQL. For example:

from myapp.models import TrackedModel

m = TrackedModel.objects.create(int_field=1, text_field="hello")
m.int_field = 2
m.save()

print(m.events.values("pgh_obj", "int_field"))

> [{'pgh_obj': 1, 'int_field': 1}, {'pgh_obj': 1, 'int_field': 2}]

Above we printed the history of int_field. We also printed pgh_obj, which references the tracked object. We'll cover how these fields and additional metadata fields are tracked later.

django-pghistory can track a subset of fields and conditionally store events based on specific field transitions. Users can also store free-form context from the application in event metadata, all with no additional database queries. See the next steps below on how to dive deeper and configure it for your use case.

Compatibility

django-pghistory is compatible with Python 3.8 - 3.12, Django 3.2 - 4.2, Psycopg 2 - 3, and Postgres 12 - 16.

Documentation

View the django-pghistory docs here to learn more about:

  • The basics and terminology.
  • Tracking historical events on models.
  • Attaching dynamic application context to events.
  • Configuring event models.
  • Aggregating events across event models.
  • The Django admin integration.
  • Reverting models to previous versions.
  • A guide on performance and scale.

There's also additional help, FAQ, and troubleshooting guides.

Installation

Install django-pghistory with:

pip3 install django-pghistory

After this, add pghistory and pgtrigger to the INSTALLED_APPS setting of your Django project.

Contributing Guide

For information on setting up django-pghistory for development and contributing changes, view CONTRIBUTING.md.

Primary Authors

Other Contributors

  • @shivananda-sahu
  • @asucrews
  • @Azurency
  • @dracos
  • @adamchainz
  • @eeriksp

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_pghistory-3.0.1.tar.gz (29.1 kB view details)

Uploaded Source

Built Distribution

django_pghistory-3.0.1-py3-none-any.whl (36.3 kB view details)

Uploaded Python 3

File details

Details for the file django_pghistory-3.0.1.tar.gz.

File metadata

  • Download URL: django_pghistory-3.0.1.tar.gz
  • Upload date:
  • Size: 29.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.12.0 Linux/5.15.0-1039-aws

File hashes

Hashes for django_pghistory-3.0.1.tar.gz
Algorithm Hash digest
SHA256 83e065b8560634f84b257d272c4d2be70c6739b712de8108bf81e8c04e40c590
MD5 9d4594470d7ed4c3cfe5174691504339
BLAKE2b-256 727f83b8018ee4906519bafbb694575d311a2aa12c1ef009ab6af22b1ed31aea

See more details on using hashes here.

File details

Details for the file django_pghistory-3.0.1-py3-none-any.whl.

File metadata

  • Download URL: django_pghistory-3.0.1-py3-none-any.whl
  • Upload date:
  • Size: 36.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.12.0 Linux/5.15.0-1039-aws

File hashes

Hashes for django_pghistory-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 902bc361cb58d6070bb4a2c52cc72fcd7461e00c3d1188f544534d6b5459d04a
MD5 9150de7dd7770b612f9decdc91066a6b
BLAKE2b-256 38059360036555fcb0d4ac61df520af21221771b96ab7dc9484a5b3ba123d06f

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