views:

59

answers:

3

Hi folks,

I need to track/log activity on the Django admin.

I know there are messages stored by admin somewhere, but I don't know how to access them in order to use them as a simple log.


I'm trying to track the following:

  • User performing the action

  • Action committed

  • Datetime of action

Thanks guys.

+2  A: 

Log is in django_admin_log table in database used by django.

zaynyatyi
@zaynyatyi: thanks! But there is no created timestamp, do you think I could fix this somehow? I'm thinking of adding an sql row with a default
RadiantHex
You can use the `action_time` inside the `LogEntry` class. Take a look at my answer,
the_void
django_admin_log table only let you that there was change it do not let you know what value is changed and secondly when using multiple language the history will show you the column name in english only ,t will not change as per you locale.It is better to make your own history or use django.revision
ha22109
+1  A: 

Take a look at the LogEntry class which stores the log for the actions inside the admin.

You could use it like this to insert custom entries in the logs:

from settings import LOG_SIZE, LOG_THRESHOLD
from django.contrib.admin.models import LogEntry

if not LogEntry._meta.installed:
        raise ImproperlyConfigured("You'll need to put 'django.contrib.admin' in your INSTALLED_APPS setting before you can use the admin application.")

def log_action(user_id, content_type_id, object_id, object_repr, action_flag, change_message=''):
        # limit log size
        log_count = LogEntry.objects.count()

        if log_count > LOG_THRESHOLD:
                to_delete = LogEntry.objects.all()[LOG_SIZE:log_count]

                #FIXME (!?): to_delete.delete()
                for d in to_delete:
                        d.delete()

        LogEntry.objects.log_action(user_id, content_type_id, object_id, object_repr, action_flag, change_message)
the_void
+1  A: 

I had to do something similar and I used something like this:

from django.contrib.admin.models import LogEntry

logs = LogEntry.objects.all() #or you can filter, etc.
for l in logs:
    #perform action

You can see all of the attributes for LogEntry, but I think the ones you are looking for are l.user, l.action_time and l.obj_repr (the name of the obj) and l.action_flag ({ 1:'Add',2:'Change',3:'Delete'}). Hope that helps!

Katharine