我想自定义删除操作管理类,并覆盖 admin.py 中的删除函数,但它不起作用!
这是错误: ID为“
我在以下站点阅读并使用了此代码,但它不起作用:
class PostAdmin(admin.ModelAdmin):
list_display = ('delete',)
def delete(self, obj):
view_name = "admin:{}_{}_delete".format(obj._meta.app_label, obj._meta.model_name)
link = reverse(view_name, args=[Post.id])
html = '<input type="button" onclick="location.href=\'{}\'" value="Delete" />'.format(link)
return format_html(html)
使用
obj.id
,而不是Post.id
class PostAdmin(admin.ModelAdmin):
list_display = ('delete',)
def delete(self, obj):
view_name = 'admin:{}_{}_delete'.format(
obj._meta.app_label, obj._meta.model_name
)
link = reverse(view_name, args=[obj.id])
return format_html(
'<input type="button" onclick="location.href=\'{}\'" value="Delete" />',
link,
)