github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

jaylett / django_audited_model

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 2
    • 1
  • Source
  • Commits
  • Network (1)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Django middleware to take care of creation/modification times & users — Read more

  cancel

http://tartarus.org/james/computers/django/

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Bugfixes: 
jaylett (author)
Sat Feb 06 04:56:21 -0800 2010
commit  b7d45b2e325512861a0ef23e756a81bfdf3adaf7
tree    b2a1e1db40f66ec345d41a657c4112a7251398b5
parent  69ea3fd6eea223c0b41eb2d8a06af26b9e48de7e
django_audited_model /
name age
history
message
file LICENSE Loading commit data...
file README
file __init__.py
file middleware.py
file models.py
README
====================
django_audited_model
====================

Django middleware to remember the current user (from the session) during a request, and use it to wire up created_by and 
modified_by fields. Also a pair of abstract models so you don't have to type the field definitions out yourself.

Use
===

Add django_audited_model.middleware.AutoCreatedAndModifiedFields to your MIDDLEWARE_CLASSES. Then add suitable fields to 
any models that need them:

Managed on creation only
------------------------

    created_by = models.ForeignKey(User, related_name='created_%(class)s_set', null=False, blank=True)

You may also like to have a second field to record the creation time:

    created_at = models.DateTimeField(null=False, blank=False, auto_now_add=True)

Managed on any modification
---------------------------

    modified_by = models.ForeignKey(User, related_name='modified_%(class)s_set', null=False, blank=True)

Similarly, a second field to record the modification time may be helpful:

    modified_at = models.DateTimeField(null=False, blank=False, auto_now=True)

"Loose" auditing
================

If modified_by and/or created_by are nullable, by setting null=True, blank=True in the field definition, then if there 
is no authenticated session user they will be left unset. If they are *not* nullable, Django will raise an exception on 
save.

Convenience abstract models
===========================

To avoid typing the field definitions in, inherit from AuditedModel or LooselyAuditedModel as required. These contain 
all four fields above.

Admin
=====

You may prefer to exclude the above fields from the admin interface, for clarity. This is *not* set up automatically by 
the abstract models; however django_audited_model.models.exclude is a list of these fields.

Use in the shell and from management commands
=============================================

Middleware is not initialised or run from the ./manage.py shell or from management commands. If you need this behaviour 
in these cases, make sure you do the following:

----------------------------------------------------------------------------
from django_audited_model.middleware import set_current_user
----------------------------------------------------------------------------

This will set up the pre-save hook which manages the fields. If you are using strict (non-nullable) fields, you will 
also need to set the current user:

----------------------------------------------------------------------------
set_current_user(u)
----------------------------------------------------------------------------

For these purposes it is often useful to have a "system" user with a low pk, so you can do something like:

----------------------------------------------------------------------------
from django.contrib.auth.models import User
set_current_user(User.objects.get(pk=1))
----------------------------------------------------------------------------

Other notes
===========

This has been extracted from running code, but not heavily tested as yet.

In the present version, for compatibility reasons, the middleware will handle created_at and modified_at itself, 
although this will then be overridden if auto_now_add or auto_now respectively are True.

The model shortcuts were originally written by Aaron Sokoloski, with some bugfixes by Andrew Godwin. The whole thing has 
been created and maintained at <http://devfort.com/>.

James Aylett <http://tartarus.org/james/computers/django/>
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server