Blogger Template by Blogcrowds.

DjangoAudit

Procedure:

  • Download audit file and save inside your project application folder.

  • Edit models file

from django.db import models
import audit

class Person(models.Model):
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)
    salary = models.PositiveIntegerField()
    history = audit.AuditTrail()

    def __str__(self):
        return "%s %s" % (self.first_name, self.last_name)

  • If you need to view the audit information in the admin interface, simply add show_in_admin=True as an argument to AuditTrail.

  • Then run python manage.py sncdb in terminal to take effect

Testing:

  • Test1View.py

from myapp.models import Person

person = Person.objects.create(first_name='Jayapal', last_name='D', salary=50000)
person.history.count()


OutPut:

1

  • Test2View.py

from myapp.models import Person

person = Person.objects.create(first_name='Jayapal', last_name='D', salary=50000)
person.salary = 65000
person.save()
for item in person.history.all():
print "%s: %s" % (item, item.salary)
person2 = Person.objects.create(first_name='Sahab', last_name='Husain', salary=50000)
print "count:", person.history.count()
print "count of person2:",person2.history.count()
print "total count in table Person:",Person.history.count()


OutPut:

Jayapal D as of 2007-08-14 20:31:21.852000: 65000
Jayapal D as of 2007-08-14 20:30:58.959000: 50000
count: 2
count of person2: 1
total count of table Person: 3

  • Tracking Information:
    As you can see, the audit trail is listed with the most recent state first. Each entry also inclues a timestamp when the edit took place. Saves and deletes are both tracked, and can be filtered on via Person.history.filter(_audit_change_type='_') . Do not use underscore, use 'I' for inserts, 'U' for updates, and 'D' for deletes. ForeignKeys? and OneToOneFields? are now supported both for saving and accessing the audit data. However, it does not archive the contents of the ForeignKey? table for the appropriate entries at the same time, and will fail if the ForeignKey? a given audit entry is related to is deleted (including if you're auditing the ForeignKey? table as well, it does not have a way to link the two audit tables together).

For More information Audit+Wiki

0 Comments:

Post a Comment



Newer Post Older Post Home