Package: baseapp-django-pghistory
This is a fork ofhttps://github.com/Opus10/django-pghistory.django-pghistorytracks changes to your Django models
usingPostgres 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-pghistoryhas 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 StartDecorate your model withpghistory.track. For example:importpghistory@pghistory.track(pghistory.Snapshot())classTrackedModel(models.Model):int_field=models.IntegerField()text_field=models.TextField()Above we’ve registered apghistory.Snapshotevent tracker toTrackedModel.
This event tracker stores every change in a dynamically-created
model that mirrors fields inTrackedModel.Runpython manage.py makemigrationsfollowed bymigrateandvoila, every change toTrackedModelis now stored. This includes bulk
methods and even changes that happen in raw SQL. For example:frommyapp.modelsimportTrackedModel# Even though we didn't declare TrackedModelEvent, django-pghistory# creates it for us in our appfrommyapp.modelsimportTrackedModelEventm=TrackedModel.objects.create(int_field=1,text_field="hello")m.int_field=2m.save()print(TrackedModelEvent.objects.values("pgh_obj","int_field"))>[{'pgh_obj':1,'int_field':1},{'pgh_obj':1,'int_field':2}]Above we printed thepgh_objfield, which is a special foreign key to the tracked
object. There are a few other specialpgh_fields that we’ll discuss later.django-pghistorycan 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.Compatibilitydjango-pghistoryis compatible with Python 3.7 - 3.11, Django 3.2 - 4.2, Psycopg 2 - 3 and Postgres 12 - 15.DocumentationView the django-pghistory docs hereto 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.InstallationInstall django-pghistory with:pip3 install django-pghistoryAfter this, addpghistoryandpgtriggerto theINSTALLED_APPSsetting of your Django project.Contributing GuideFor information on setting up django-pghistory for development and
contributing changes, viewCONTRIBUTING.rst.Primary Authors@wesleykendall (Wes Kendall,wesleykendall@protonmail.com)Other Contributors@shivananda-sahu@asucrews@Azurency@dracos@adamchainz@eeriksp