python - 从DoesNotExist异常实例获取查找参数

标签 python django

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/

相关文章:

python - TemplateView 的 post() 到 get_context_data() 方法的变量

django - 如何在 django-piston 剩余输出中显示外键而不是相关对象数据

python - AWS Lambda 导入模块失败

python - Windows 上的 django-admin.py 和 virtualenv 问题

python - Pandas 将分钟数字索引(0 到 1440)转换为日期时间

python - 使用 file.write 的 Python 语法无效

django - 防止 Django 模板中的整数出现 NUMBER_GROUPING

python - bool 字段总是保存 False django

python - 你可以在 Python 中对线程或进程进行排队吗?

python - 在 Matplotlib 中绘制两点之间的简单直线