Wednesday, June 1, 2011

Re: Auditing Record Changes in All Admin Tables

Bingo! Everything's working. Here's the complete solution for others:

models.py
-------------
from django.db import models
from django.contrib import admin

class AuditedTable(models.Model):
created = models.DateTimeField(auto_now_add=True)
created_by =
models.CharField(blank=True,max_length=20,editable=False)
updated = models.DateTimeField(auto_now=True)
updated_by =
models.CharField(blank=True,max_length=20,editable=False)
class Meta:
abstract = True

class Entity1(AuditedTable):
title = models.CharField(max_length=20)
...

admin.py
-----------
from myapp.models import Entity1
from django.contrib import admin
from django.db import models

class AuditAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
if change:
obj.updated_by = request.user.username
else:
obj.created_by = request.user.username
obj.save()

class Entity1Admin(AuditAdmin):
list_display =
('title','created','created_by','updated','updated_by')
readonly_fields = ('created','created_by','updated','updated_by')

admin.site.register(Entity1,Entity1Admin)

------------
Thanks again for the excellent help Ryan.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment