可以通过Django的管理员监控django_admin_log吗?

时间:2021-02-16 19:21:05

The table django_admin_log is useful to monitor the actions of users in the admin. Right now, I can achieve that by querying the database directly. Is there a built-in functionality where I can view the table django_admin_log through Django's admin for all users?

表django_admin_log可用于监视管理员中用户的操作。现在,我可以通过直接查询数据库来实现这一目标。是否有内置功能,我可以通过Django的管理员为所有用户查看表django_admin_log?

3 个解决方案

#1


8  

Can't you just:

你不能只:

from django.contrib.admin.models import LogEntry
admin.site.register(LogEntry)

In one of your admin.py files? I just tested it and it is barebones but it works.

在你的一个admin.py文件中?我只是测试它,它是准系统,但它的工作原理。

You might want to be more specific and create a ModelAdmin class for LogEntry to provide for a better list view and maybe some filtering abilities. But that should work.

您可能希望更具体,并为LogEntry创建一个ModelAdmin类,以提供更好的列表视图和一些过滤功能。但这应该有效。

#2


7  

Here's my version. Reference for fields available.

这是我的版本。可用字段的参考。

class LogAdmin(admin.ModelAdmin):
    """Create an admin view of the history/log table"""
    list_display = ('action_time','user','content_type','change_message','is_addition','is_change','is_deletion')
    list_filter = ['action_time','user','content_type']
    ordering = ('-action_time',)
    #We don't want people changing this historical record:
    def has_add_permission(self, request):
        return False
    def has_change_permission(self, request, obj=None):
        #returning false causes table to not show up in admin page :-(
        #I guess we have to allow changing for now
        return True
    def has_delete_permission(self, request, obj=None):
        return False

#3


1  

Here's a more extensive Django 2 compatible admin configuration for viewing all log entries:

这是一个更广泛的Django 2兼容管理配置,用于查看所有日志条目:

from django.contrib import admin
from django.contrib.admin.models import LogEntry, DELETION
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.urls import reverse

@admin.register(LogEntry)
class LogEntryAdmin(admin.ModelAdmin):
    date_hierarchy = 'action_time'
    readonly_fields = [field.name for field in LogEntry._meta.get_fields()]
    list_filter = ['user', 'content_type']
    search_fields = ['object_repr', 'change_message']
    list_display = ['__str__', 'content_type', 'action_time', 'user', 'object_link']

    def has_add_permission(self, request):
        return False

    def has_change_permission(self, request, obj=None):
        # only for superusers, cannot return False, the module
        # wouldn't be visible in admin
        return request.user.is_superuser and request.method != 'POST'

    def has_delete_permission(self, request, obj=None):
        return False

    def object_link(self, obj):
        if obj.action_flag == DELETION:
            link = obj.object_repr
        else:
            ct = obj.content_type
            try:
                link = mark_safe('<a href="%s">%s</a>' % (
                                 reverse('admin:%s_%s_change' % (ct.app_label, ct.model),
                                         args=[obj.object_id]),
                                 escape(obj.object_repr),
                ))
            except NoReverseMatch:
                link = obj.object_repr
        return link
    object_link.admin_order_field = 'object_repr'
    object_link.short_description = 'object'

    def queryset(self, request):
        return super(LogEntryAdmin, self).queryset(request) \
            .prefetch_related('content_type')

It is based on this Django snippet and the result looks like this:

它基于这个Django片段,结果如下:

可以通过Django的管理员监控django_admin_log吗?

If you want to go the extra mile, you can remove save buttons from change view as follows.

如果您想加倍努力,可以从更改视图中删除保存按钮,如下所示。

Add custom change form template to admin.py:

将自定义更改表单模板添加到admin.py:

@admin.register(LogEntry)
class LogEntryAdmin(admin.ModelAdmin):

    change_form_template = 'administration/remove-save-buttons.html'

Leave submit_buttons_bottom block empty in the custom change form template administration/templates/administration/remove-save-buttons.html:

在自定义更改表单模板administration / templates / administration / remove-save-buttons.html中将submit_buttons_bottom块保留为空:

{% extends 'admin/change_form.html' %}

{% block submit_buttons_bottom %}
{% endblock %}

#1


8  

Can't you just:

你不能只:

from django.contrib.admin.models import LogEntry
admin.site.register(LogEntry)

In one of your admin.py files? I just tested it and it is barebones but it works.

在你的一个admin.py文件中?我只是测试它,它是准系统,但它的工作原理。

You might want to be more specific and create a ModelAdmin class for LogEntry to provide for a better list view and maybe some filtering abilities. But that should work.

您可能希望更具体,并为LogEntry创建一个ModelAdmin类,以提供更好的列表视图和一些过滤功能。但这应该有效。

#2


7  

Here's my version. Reference for fields available.

这是我的版本。可用字段的参考。

class LogAdmin(admin.ModelAdmin):
    """Create an admin view of the history/log table"""
    list_display = ('action_time','user','content_type','change_message','is_addition','is_change','is_deletion')
    list_filter = ['action_time','user','content_type']
    ordering = ('-action_time',)
    #We don't want people changing this historical record:
    def has_add_permission(self, request):
        return False
    def has_change_permission(self, request, obj=None):
        #returning false causes table to not show up in admin page :-(
        #I guess we have to allow changing for now
        return True
    def has_delete_permission(self, request, obj=None):
        return False

#3


1  

Here's a more extensive Django 2 compatible admin configuration for viewing all log entries:

这是一个更广泛的Django 2兼容管理配置,用于查看所有日志条目:

from django.contrib import admin
from django.contrib.admin.models import LogEntry, DELETION
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.urls import reverse

@admin.register(LogEntry)
class LogEntryAdmin(admin.ModelAdmin):
    date_hierarchy = 'action_time'
    readonly_fields = [field.name for field in LogEntry._meta.get_fields()]
    list_filter = ['user', 'content_type']
    search_fields = ['object_repr', 'change_message']
    list_display = ['__str__', 'content_type', 'action_time', 'user', 'object_link']

    def has_add_permission(self, request):
        return False

    def has_change_permission(self, request, obj=None):
        # only for superusers, cannot return False, the module
        # wouldn't be visible in admin
        return request.user.is_superuser and request.method != 'POST'

    def has_delete_permission(self, request, obj=None):
        return False

    def object_link(self, obj):
        if obj.action_flag == DELETION:
            link = obj.object_repr
        else:
            ct = obj.content_type
            try:
                link = mark_safe('<a href="%s">%s</a>' % (
                                 reverse('admin:%s_%s_change' % (ct.app_label, ct.model),
                                         args=[obj.object_id]),
                                 escape(obj.object_repr),
                ))
            except NoReverseMatch:
                link = obj.object_repr
        return link
    object_link.admin_order_field = 'object_repr'
    object_link.short_description = 'object'

    def queryset(self, request):
        return super(LogEntryAdmin, self).queryset(request) \
            .prefetch_related('content_type')

It is based on this Django snippet and the result looks like this:

它基于这个Django片段,结果如下:

可以通过Django的管理员监控django_admin_log吗?

If you want to go the extra mile, you can remove save buttons from change view as follows.

如果您想加倍努力,可以从更改视图中删除保存按钮,如下所示。

Add custom change form template to admin.py:

将自定义更改表单模板添加到admin.py:

@admin.register(LogEntry)
class LogEntryAdmin(admin.ModelAdmin):

    change_form_template = 'administration/remove-save-buttons.html'

Leave submit_buttons_bottom block empty in the custom change form template administration/templates/administration/remove-save-buttons.html:

在自定义更改表单模板administration / templates / administration / remove-save-buttons.html中将submit_buttons_bottom块保留为空:

{% extends 'admin/change_form.html' %}

{% block submit_buttons_bottom %}
{% endblock %}