Skip to main content

History tracking for Django and Postgres (updated for Baseapp)

Project description

This is a fork of https://github.com/Opus10/django-pghistory.

django-pghistory tracks changes to your Django models using Postgres triggers. It offers several advantages over other apps:

  • No base models or managers to inherit, no signal handlers, and no custom save methods. All changes are reliably tracked, including bulk methods, with miniscule code.

  • Snapshot all changes to your models, create conditional event trackers, or only track the fields you care about.

  • Changes are stored in structured event tables that mirror your models. No JSON, and you can easily query events in your application.

  • Changes can be grouped together with additional context attached, such as the logged-in user. The middleware can do this automatically.

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

Quick Start

Decorate your model with pghistory.track. For example:

import pghistory

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

Above we’ve registered a pghistory.Snapshot event tracker to TrackedModel. This event tracker stores every change in a dynamically-created model that mirrors fields in TrackedModel.

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

# Even though we didn't declare TrackedModelEvent, django-pghistory
# creates it for us in our app
from myapp.models import TrackedModelEvent

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

print(TrackedModelEvent.objects.values("pgh_obj", "int_field"))

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

Above we printed the pgh_obj field, which is a special foreign key to the tracked object. There are a few other special pgh_ fields that we’ll discuss 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 that’s referenced by the event model, 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.7 - 3.11, Django 3.2 - 4.2, Psycopg 2 - 3 and Postgres 12 - 15.

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.rst.

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

baseapp_django_pghistory-2.8.1.tar.gz (32.9 kB view details)

Uploaded Source

Built Distribution

baseapp_django_pghistory-2.8.1-py3-none-any.whl (39.7 kB view details)

Uploaded Python 3

File details

Details for the file baseapp_django_pghistory-2.8.1.tar.gz.

File metadata

  • Download URL: baseapp_django_pghistory-2.8.1.tar.gz
  • Upload date:
  • Size: 32.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.12 Linux/6.4.6-76060406-generic

File hashes

Hashes for baseapp_django_pghistory-2.8.1.tar.gz
Algorithm Hash digest
SHA256 5aaeefeb8e0d8eed794c0a1686b7cc46593b85dcbb52f8d5882f6ec3485fe0de
MD5 bfc1e531154770fcc4ea6e74c80f002b
BLAKE2b-256 748afc3963d09bdf31b045b7b6f3f9dd325111afe3968f3f85ae047974ab00a1

See more details on using hashes here.

File details

Details for the file baseapp_django_pghistory-2.8.1-py3-none-any.whl.

File metadata

File hashes

Hashes for baseapp_django_pghistory-2.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f891a22ad97383d82e22c6ae6059079cd6f4b335cb2284d8556970fd2d6b2a70
MD5 106568138423163786f267dad77be16d
BLAKE2b-256 0ee50c332326526f45ea19911fdedf45aaa22ca9590a00ae88efc9ff56d624a6

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