Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Django Audited Models:

author:

Chris Chambers

date:

2012/05/08

Django Audited Models provides a simple abstract base class (and factory function) for tracking database record creation and modification times, as well as the creator of the record and the last user to edit it. It leverages two pluggable applications to achieve this:

  1. Django Extensions, which provides (amongst many other things) a CreationDateTimeField and ModificationDateTimeField.

  2. Django Threaded Multihost, originally developed for Satchmo, which provides a threadlocals mechanism that works reliably for Django installations powering multiple sites via the sites framework.

Installation

  1. Install pip.

  2. Run pip install django-audited-models.

  3. Add threaded_multihost.middleware.ThreadLocalMiddleware to your list of MIDDLEWARE_CLASSES.

  4. Inherit from AuditedModel instead of models.Model in your django applications.

  5. Profit!

Explanation

Django-audited-models fulfils several needs:

  1. This kind of metadata is almost always useful, and inexpensive to capture - most of your clients will just presume this information is logged and will be frustrated if they can’t get at it (“What do you mean we can’t see who created this record?”)

  2. Django’s admin logging functionality provides some of this detail, but is flawed in that it only captures events which take place within the admin itself.

  3. This app provides a consistent, logical naming convention and interface for the metadata.

  4. This app makes use of several other applications to capture this information automatically. No need to pollute your views with unrelated logic or override ModelAdmin methods to store the user who created the record.

  5. It’s pluggable - simply drop it in, install its requirements, inherit from AuditedModel, and you’ll have your creation/modification dates and the users responsible for them, respectively. Similarly, replace your ModelAdmin with the AuditedAdmin subclass and you’ll have some sensible defaults for the admin UI (readonly metadata fields, etc.).

  6. As a bonus, ensures that MyModel.objects.latest() does something sensible by default - very handy when working with the interpreter, especially.

Some might question the verbosity of the time entry fields (datetime_created and datetime_modified). Consider the following:

>>> from datetime import date
>>> latest_user = User.objects.latest('date_joined')
>>> if latest_user.date_joined < date.today():
>>>     print "Nobody has joined the site today."

# Intuitively, this looks like it will work, but...
TypeError: can't compare datetime.datetime to datetime.date

Python treats datetime objects very differently to dates, and the explicit fieldnames remind the developer of this difference and help prevent errors due to incorrect assumptions.

Requirements

  • Python 2.5+

  • Django 1.2+

  • Applications listed in requirements.txt

You will also need to install the applications listed in requirements-dev.txt in order to run the test suite.

Metadata

Release files for django-audited-models 0.4.alpha

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-audited-models 0.4.alpha
File Size Uploaded Provenance
django-audited-models-0.4.alpha.tar.gz 4.5 kB Without provenance Details

Release files / django-audited-models-0.4.alpha.tar.gz

Download URL django-audited-models-0.4.alpha.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
e06f79215c1d015ec1288d7339c0ae1c83e3db1b38e52bd3ba4120ee9d1dbcbe
BLAKE2b-256 checksum
How to use checksums
7cd80733f6b721ba170bab26d9926cc6511ff93302bd921f78c2fa2968a245f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for django-audited-models 0.4.alpha

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page