Djangoadmin⽇志管理
django拥有⾃⼰的admin页⾯,同时它有⾃⼰的⼀套⽇志管理结构。正常的⽇志存放在django_admin_log表中,查看⽇志可以直接在django的管理页⾯查看。然⽽,它的⽇志记录⽐较简单,且存放在数据库中。如何扩展他呢?
由于django的管理model,均采⽤类似⽅法:
class ApplicationAdmin(admin.ModelAdmin):
list_display = ('id','key','name', 'description', 'type','apphandler','argument','create','modified')
search_fields = ('key','name','description')
ister(Application,ApplicationAdmin)
实际的⽇志记录逻辑均在⽗类ModelAdmin中实现,具体在ib.admin.options中。
def log_addition(self, request, object):
"""
Log that an object has been successfully added.
The default implementation creates an admin LogEntry object.
"""
dels import LogEntry, ADDITION
LogEntry.objects.log_action(
user_id = request.user.pk,
content_type_id = _for_model(object).pk,
object_id = object.pk,
object_repr = force_unicode(object),
action_flag = ADDITION
)
def log_change(self, request, object, message):
"""
Log that an object has been successfully changed.
The default implementation creates an admin LogEntry object.
"""
dels import LogEntry, CHANGE
LogEntry.objects.log_action(
user_id = request.user.pk,django admin 自定义页面
content_type_id = _for_model(object).pk,
object_id = object.pk,
object_repr = force_unicode(object),
action_flag = CHANGE,
change_message = message
)
def log_deletion(self, request, object, object_repr):
"""
Log that an object will be deleted. Note that this method is called
before the deletion.
The default implementation creates an admin LogEntry object.
"""
dels import LogEntry, DELETION
LogEntry.objects.log_action(
user_id = request.user.id,
content_type_id = _for_del).pk,
object_id = object.pk,
object_repr = object_repr,
action_flag = DELETION
)
⼤家⼀看就明⽩,下⼀步做什么了,直接覆写,或是拦截⼀下,修改⼀下message信息:
class DemoAdmin(admin.ModelAdmin):
list_display = ('id', 'name', 'description', 'create','modified')
search_fields = ('name', 'description',)
def log_change(self, request, object, message):
newmessage= "xxxx" //将消息改写为xxx
super(SpaceAdmin, self).log_change(request, object, newmessage)
class Meta:
pass
class Admin:
这样就可以完成⽇志改写了,或者直接叫个log也没啥问题。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论