: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:
Django Extensions
_, which provides (amongst many other things) a
CreationDateTimeField
and ModificationDateTimeField
.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... Django Extensions
: https://github.com/django-extensions/django-extensions
.. Django Threaded Multihost
: https://bitbucket.org/bkroeze/django-threaded-multihost
.. _Satchmo: http://www.satchmoproject.com/
pip install django-audited-models
.threaded_multihost.middleware.ThreadLocalMiddleware
to your list of
MIDDLEWARE_CLASSES
.AuditedModel
instead of models.Model
in your django
applications... _pip: http://www.pip-installer.org/en/latest/index.html .. _setuptools: http://pypi.python.org/pypi/setuptools
Django-audited-models fulfils several needs:
ModelAdmin
methods to store the user who created the
record.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.).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.txt
You will also need to install the applications listed in
requirements-dev.txt
in order to run the test suite.