Django admin - how to get object deletion link in

2019-08-31 05:45发布

Is there any possibility to get object delete url in Django Admin, in change list template (change_list_results.html)? I must add icons (edit/delete) on results list and try to get that links for object.

1条回答
Bombasti
2楼-- · 2019-08-31 06:38

The delete url for a particular instance can be obtained via:

info = obj._meta.app_label, obj._meta.module_name
reverse('admin:%s_%s_delete' % info, args=(obj.id,))

So, just add a method to your ModelAdmin to return that URL wrapped in appropriate HTML:

def delete_link(self, obj):
    info = obj._meta.app_label, obj._meta.module_name
    url = reverse('admin:%s_%s_delete' % info, args=(obj.id,))
    return '<a href="%s">Delete</a>' % url
delete_link.allow_tags = True
delete_link.short_description = 'Delete'

And, then add that to list_display, like any other field on your model. 

查看更多
登录 后发表回答