记录 Django 管理员的活动 - Django

发布于 2024-09-08 04:32:15 字数 203 浏览 5 评论 0原文

我需要跟踪/记录 Django 管理员的活动。

我知道管理员在某处存储了消息,但我不知道如何访问它们以便将它们用作简单的日志。


我正在尝试跟踪以下内容:

  • 执行操作的用户

  • 已提交的操作

  • 操作的日期时间

谢谢大家。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彼岸花似海 2024-09-15 04:32:15

我必须做类似的事情,我使用了这样的东西:

from django.contrib.admin.models import LogEntry

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

你可以看到 LogEntry 的所有属性,但我认为您正在寻找的属性是 l.userl.action_timel .obj_reprobj 的名称)和 l.action_flag ({ 1:'Add',2:'Change',3: '删除'})。希望有帮助!

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!

顾北清歌寒 2024-09-15 04:32:15

日志位于 django 使用的数据库中的 django_admin_log 表中。

Log is in django_admin_log table in database used by django.

优雅的叶子 2024-09-15 04:32:15

看一下 LogEntry 类,它存储管理员内部操作的日志。

您可以像这样使用它在日志中插入自定义条目:

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文