from django.contrib.admin.models import LogEntry from django.core.management.base import BaseCommand class Command(BaseCommand): help = "Display recent actions performed via the admin app" args = "[number of actions to display]" requires_model_validation = False def handle(self, how_many=5, *args, **options): last_actions = LogEntry.objects.all()[:how_many] for action in last_actions: timestamp = action.action_time.strftime("%Y-%m-%d %H:%M:%S") print '%s: %s %s %s: "%s"' % (timestamp, action.user, verb(action), action.content_type, action.object_repr) def verb(action): """Return English label for a given action code""" if action.is_deletion(): return "deleted" elif action.is_addition(): return "added" elif action.is_change(): return "changed"