我有一个Django应用程序,其模型如下:
class Foo(models.Model):
name = models.CharField(max_length=100)
我在这个模型中有一个特定的实例,它的名字是'bar'(例如),我想阻止这个实例被删除。
我已经创建了一个这样的信号接收器:
def protect_foo_bar(sender, instance, using, **kwargs):
if instance.title != 'bar':
pass
else:
raise ProtectedError(protected_objects=instance, msg='You cannot delete this object')
我已将此接收器连接到pre_delete
信号,如下所示:
pre_delete.connect(receiver=protect_foo_bar, dispatch_uid='protect_foo_bar_signal',
sender='app_name.foo')
当我尝试从Django的管理面板中删除此特定对象时,它会返回一个异常(错误500)。是否可以强制管理面板显示you cannot delete this object
之类的错误,而不是向用户返回异常?
修改
这是追溯:
Traceback:
File "C:\Users\asus\PycharmProjects\AleTaha\env\lib\site-packages\django\core\handlers\exception.py" in inner
41. response = get_response(request)
File "C:\Users\asus\PycharmProjects\AleTaha\env\lib\site-packages\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "C:\Users\asus\PycharmProjects\AleTaha\env\lib\site-packages\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\asus\PycharmProjects\AleTaha\env\lib\site-packages\django\contrib\admin\options.py" in wrapper
551. return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Users\asus\PycharmProjects\AleTaha\env\lib\site-packages\django\utils\decorators.py" in _wrapped_view
149. response = view_func(request, *args, **kwargs)
File "C:\Users\asus\PycharmProjects\AleTaha\env\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
57. response = view_func(request, *args, **kwargs)
File "C:\Users\asus\PycharmProjects\AleTaha\env\lib\site-packages\django\contrib\admin\sites.py" in inner
224. return view(request, *args, **kwargs)
File "C:\Users\asus\PycharmProjects\AleTaha\env\lib\site-packages\django\utils\decorators.py" in _wrapper
67. return bound_func(*args, **kwargs)
File "C:\Users\asus\PycharmProjects\AleTaha\env\lib\site-packages\django\utils\decorators.py" in _wrapped_view
149. response = view_func(request, *args, **kwargs)
File "C:\Users\asus\PycharmProjects\AleTaha\env\lib\site-packages\django\utils\decorators.py" in bound_func
63. return func.__get__(self, type(self))(*args2, **kwargs2)
File "C:\Users\asus\PycharmProjects\AleTaha\env\lib\site-packages\django\contrib\admin\options.py" in changelist_view
1584. response = self.response_action(request, queryset=cl.get_queryset(request))
File "C:\Users\asus\PycharmProjects\AleTaha\env\lib\site-packages\django\contrib\admin\options.py" in response_action
1286. response = func(self, request, queryset)
File "C:\Users\asus\PycharmProjects\AleTaha\env\lib\site-packages\django\contrib\admin\actions.py" in delete_selected
49. queryset.delete()
File "C:\Users\asus\PycharmProjects\AleTaha\env\lib\site-packages\django\db\models\query.py" in delete
614. deleted, _rows_count = collector.delete()
File "C:\Users\asus\PycharmProjects\AleTaha\env\lib\site-packages\django\db\models\deletion.py" in delete
279. sender=model, instance=obj, using=self.using
File "C:\Users\asus\PycharmProjects\AleTaha\env\lib\site-packages\django\dispatch\dispatcher.py" in send
193. for receiver in self._live_receivers(sender)
File "C:\Users\asus\PycharmProjects\AleTaha\env\lib\site-packages\django\dispatch\dispatcher.py" in <listcomp>
193. for receiver in self._live_receivers(sender)
File "C:/Users/asus/PycharmProjects/AleTaha\content\signals.py" in protect_content_category
8. raise ProtectedError(protected_objects=instance, msg='دستهی «همه» را نمیتوان حذف کرد.')
Exception Type: ProtectedError at /admin/content/contentcategory/
Exception Value: ('دسته\u200cی «همه» را نمی\u200cتوان حذف کرد.', <ContentCategory: همه>)
答案 0 :(得分:1)
是的,您可以覆盖delete_view()
的{{1}}和response_action()
方法。我还建议覆盖ModelAdmin
方法,以便根本不显示“删除”按钮:
has_delete_permission()