简体   繁体   English

除非符合条件,否则如何在Django admin中显示删除确认

[英]How do I not show delete confirmation in Django admin unless conditions are met

I overrode the delete_queryset method to add a condition before being able to delete a model. delete_querysetdelete_queryset方法以在能够删除模型之前添加条件。 It looks like this: 它看起来像这样:

def delete_queryset(self, request, queryset):
    if condition_is_not_met:
        self.message_user(request, 'Error', level=messages.ERROR)
        return

    super().delete_queryset(request, queryset)

This DOES prevent the deletion but it shows that error message and below it says, Successfully deleted 'x' objects (Again, not deleted so success message should not be there). 这可以防止删除,但它显示错误消息,并在其下面显示, Successfully deleted 'x' objects (再次,未删除,因此成功消息不应该存在)。 I don't want to display that error message and what would be even better is if the confirmation page did not even show up if the condition is not met (This is a bonus though). 我不想显示该错误消息,如果确认页面甚至没有显示,如果条件不满足,那么更好的是(虽然这是奖金)。 Any suggestions? 有什么建议么?

Just override the has_delete_permission method on modeladmin, which will return True or False based on the condition. 只需覆盖has_delete_permission上的has_delete_permission方法,该方法将根据条件返回True或False。

def has_delete_permission(self, request, obj=None):
    if condition_is_not_met:
        return False
    return True

Docs 文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何跳过特定模型的Django admin中的删除确认页面? - How to skip delete confirmation page in Django admin for specific model? 如何覆盖 django 管理站点中的删除确认页面? - how to override delete confirmation page in django admin site? 扩展 Django 管理员删除确认页面 - Extending Django admin delete confirmation page 我如何有条件地否决Django Admin 1.5中的删除尝试? - How do I conditionally veto a delete attempt in Django Admin 1.5? 如何使用 Bootstrap4 模态在 Django 中制作功能正常的删除确认弹出窗口 - How do i make a functioning Delete confirmation popup in Django using Bootstrap4 Modal 如何获取显示的Django管理按钮快捷方式 - How do I get the Django admin buttons shortcuts to show 如何在 Django 中使用 bootstrap Modal 对表数据进行删除确认? - How to do Delete confirmation for a table data with bootstrap Modal in Django? 满足条件后如何让消息框出现? - How do I make the messagebox appear after the conditions are met? 如何:单击按钮,然后执行操作,然后在Django中显示确认 - How to: Click button, then do stuff, then show confirmation in Django 如何在不同的语句中循环多个条件,如果条件不满足则使其循环? - How do I loop multiple conditions in different statements, and make it loop If the conditions are not met?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM