表django_admin_log
对于监控管理用户的操作非常有用。目前,我可以通过直接查询数据库来实现这一点。是否存在内置功能,使我能够通过Django的管理界面查看所有用户的django_admin_log
表?
表django_admin_log
对于监控管理用户的操作非常有用。目前,我可以通过直接查询数据库来实现这一点。是否存在内置功能,使我能够通过Django的管理界面查看所有用户的django_admin_log
表?
你不能只做这个吗:
from django.contrib.admin.models import LogEntry
admin.site.register(LogEntry)
在你的admin.py文件中吗?我刚测试了一下,虽然很简单但它可以工作。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
readonly_fields = ['user', 'content_type', 'object_id', 'object_repr', 'action_flag', 'change_message']
- 您仍然可以单击保存,但无法进行任何更改。 - EvdB以下是一个更详细的管理员配置,用于查看所有日志条目,并依赖于Django 2.1的仅视图权限:
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 = ('action_time',)
list_filter = ['user', 'content_type']
search_fields = ['object_repr', 'change_message']
list_display = ['__str__', 'content_type', 'action_time', 'user', 'object_link']
# keep only view permission
def has_add_permission(self, request):
return False
def has_change_permission(self, request, obj=None):
return False
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')
raw_id_fields
.I also created filter for action as orignal model does not have CHOICES
field and using just action_flag
as filter will show 1,2,3 as choices, which isn't very user friendly:
class ActionFlagFilter(admin.SimpleListFilter):
title = 'Action flag filter'
parameter_name = 'action_flag'
def lookups(self, request, model_admin):
return (
(ADDITION, "Add"),
(CHANGE, "Change"),
(DELETION, "Delete"),
)
def queryset(self, request, queryset):
if self.value():
return queryset.filter(action_flag=self.value())
return queryset
KeyError at /admin/ Log entries
错误,我不知道该如何解决它。 - mh-firouzjah