实际上,现在有一种比接受的答案更简单的方法,只要您有一个ModelAdmin
可用的方法,您可以在自定义操作的情况下执行此操作。
该类ModelAdmin
没有很好的文档记录,但它实际上提供了这些方法作为快捷方式:
def log_addition(self, request, object, message):
"""
Log that an object has been successfully added.
The default implementation creates an admin LogEntry object.
"""
def log_change(self, request, object, message):
"""
Log that an object has been successfully changed.
The default implementation creates an admin LogEntry object.
"""
def log_deletion(self, request, object, object_repr):
"""
Log that an object will be deleted. Note that this method must be
called before the deletion.
The default implementation creates an admin LogEntry object.
"""
这些都很容易使用。继续问题中的示例:
def make_checked(modeladmin, request, queryset):
queryset.update(checked = 1)
for obj in queryset:
modeladmin.log_change(request, obj, 'Marked checked: ' + str(obj))
make_checked.short_description = 'Mark selected products as checked'