• Stars
    star
    322
  • Rank 130,398 (Top 3 %)
  • Language
    Python
  • License
    BSD 3-Clause "New...
  • Created over 4 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Track historical events to Django models using Postgres triggers.

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

More Repositories

1

django-pgtrigger

Write Postgres triggers for your Django models
Python
524
star
2

django-pgtransaction

A context manager/decorator which extends Django's atomic function with the ability to set isolation level and retries for a given transaction.
Python
51
star
3

django-pgbulk

Native postgres bulk update and upsert operations.
Python
50
star
4

django-pgclone

Dump and restore Postgres databases with Django.
Python
34
star
5

git-tidy

Tidy git commit messages, linting, and logging
Python
28
star
6

django-pglock

Postgres advisory locks, table locks, and blocking lock management.
Python
26
star
7

django-pgstats

Commands and models for tracking internal postgres stats.
Python
19
star
8

django-pgconnection

Route postgres connections and hook into cursor execution
Python
17
star
9

django-migration-docs

Sync and validate additional information about your Django migrations.
Python
12
star
10

django-pgmigrate

Avoid costly downtime during Postgres migrations.
Python
11
star
11

qik

Run cached commands in your Python monorepo.
Python
11
star
12

django-strict-fields

A collection of fields and utilities to help make model fields more strict.
Python
10
star
13

django-pgactivity

View, filter, and kill Postgres queries.
Python
10
star
14

formaldict

Formal structured dictionaries parsed from a schema
Python
8
star
15

footing

Keep templated projects in sync with their template
Python
7
star
16

python-library-template

The template for all public Django apps by Opus 10
Python
4
star
17

detail

Build automations off of structured notes in your project
Python
3
star
18

public-python-library-template

The template for all public python packages by Opus 10
Python
2
star
19

django-pgpartition

Partition Django models.
Python
1
star
20

django-pgstrictjson

JSON fields with trigger-based validation.
Python
1
star