gpt4 book ai didi

python - 捕获任何 DoesNotExist 错误

转载 作者:行者123 更新时间:2023-11-28 20:44:35 26 4
gpt4 key购买 nike

我正在使用 Django 1.7。通常你可以在你的模型上捕获 DoesNotExist 异常;

try:
...
except model.DoesNotExist, den:
...

我想捕获任何 DoesNotExist 异常。我真的不想关心它的模型。实际上,我也真的不知道 DoesNotExist 是通过哪个模型通过代码段的。我的意思是,我不知道模型。

所以我必须以某种方式捕获任何 DoesNotExist 错误。

有没有办法在 Django 中捕获任何 DoesNotExist 错误?

最佳答案

DoesNotExist 异常是 django.core.exceptions.ObjectDoesNotExist 的子类:

from django.core.exceptions import ObjectDoesNotExist

try:
# ...
except ObjectDoesNotExist as den:
# handle exception

关于python - 捕获任何 DoesNotExist 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25928990/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com