Logging activity on Django's admin - Django

前端 未结 3 1983
甜味超标
甜味超标 2020-12-25 11:41

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

3条回答
  •  长情又很酷
    2020-12-25 11:58

    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)
    

提交回复
热议问题