Django tutorial有一个部分显示了一些引发 DoesNotExist
异常的代码:
# Request an ID that doesn't exist, this will raise an exception.
>>> Poll.objects.get(id=2)
Traceback (most recent call last):
...
DoesNotExist: Poll matching query does not exist. Lookup parameters were {'id': 2}
异常实例上的消息显示用于查找的参数。但我没有看到这些参数:
>>> django.get_version()
'1.5.6'
>>> Client.objects.get(pk=2)
Traceback (most recent call last):
...
DoesNotExist: Client matching query does not exist.
实际上,这些信息对于我的项目调试日志记录非常有帮助。为什么我们看不到它,如果可能的话如何重新打开它?
最佳答案
本教程是为 Django 1.5 编写的。当时异常比较详细,source :
raise self.model.DoesNotExist(
"%s matching query does not exist. "
"Lookup parameters were %s" %
(self.model._meta.object_name, kwargs))
但是,在最新版本中,异常消息已更改,现在仅包含 source :
raise self.model.DoesNotExist(
"%s matching query does not exist." %
self.model._meta.object_name)
仅供引用,实际的changeset尝试修复Passing self to object query may cause infinite regression问题。
关于python - 从DoesNotExist异常实例获取查找参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25064423/