django 添加adminlog view

Posted mulinux

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django 添加adminlog view相关的知识,希望对你有一定的参考价值。

默认的log view

vi admin.py

 

from django.contrib.admin.models import LogEntry

admin.site.register(LogEntry)

 

相对较全的log view

vi admin.py

from django.contrib.admin.models import LogEntry, DELETION
from django.utils.html import escape
from django.core.urlresolvers import reverse


class LogEntryAdmin(admin.ModelAdmin):

    date_hierarchy = ‘action_time‘

    readonly_fields = LogEntry._meta.get_all_field_names()

    list_filter = [
        ‘user‘,
        ‘content_type‘,
        ‘action_flag‘
    ]

    search_fields = [
        ‘object_repr‘,
        ‘change_message‘
    ]


    list_display = [
        ‘action_time‘,
        ‘user‘,
        ‘content_type‘,
        ‘object_link‘,
        ‘action_flag‘,
        ‘change_message‘,
    ]

    def has_add_permission(self, request):
        return False

    def has_change_permission(self, request, obj=None):
        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 = escape(obj.object_repr)
        else:
            ct = obj.content_type
            link = u‘<a href="%s">%s</a>‘ % (
                reverse(‘admin:%s_%s_change‘ % (ct.app_label, ct.model), args=[obj.object_id]),
                escape(obj.object_repr),
            )
        return link
    object_link.allow_tags = True
    object_link.admin_order_field = ‘object_repr‘
    object_link.short_description = u‘object‘
    
    def queryset(self, request):
        return super(LogEntryAdmin, self).queryset(request)             .prefetch_related(‘content_type‘)


admin.site.register(LogEntry, LogEntryAdmin)

 参考:https://djangosnippets.org/snippets/2484/

 

以上是关于django 添加adminlog view的主要内容,如果未能解决你的问题,请参考以下文章

Django 管理模型 add_view:如何删除“保存并添加另一个”按钮?

Django 向 auth_view 添加消息

将 CSS 类添加到 django.contrib.auth.views.login 中的字段

如何在 django as_view() 方法中添加我自己的属性?

Django入门2

将链接或任何 html 标记添加到 django-datatables-view 中的列