[英]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_queryset
了delete_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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.