Logging activity on Django’s admin

Django has inbuilt capability to log actions into the database for auditing purposes. Here’s a sample how this can be implemented:

from django.contrib.admin.models import LogEntry, CHANGE
from django.contrib.contenttypes.models import ContentType
from django.shortcuts import get_object_or_404

#Inside your view or class. The view should have this on top of the definition "@log_info"
tenant = get_object_or_404(InstallationTenant, pk=tenant_pk)  #InstallationTenant is the model
    user_id         = request.user.id, 
    content_type_id = ContentType.objects.get_for_model(tenant).pk,
    object_id       = tenant.pk,
    object_repr     = force_unicode(tenant),  # or tenant.__unicode__()
    action_flag     = CHANGE

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s