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

时间:2014-07-31 16:35:31

标签: 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.

实际上,这些信息对我的项目调试日志记录非常有帮助。为什么我们没有看到它,如果可能的话怎么能重新开启呢?

1 个答案:

答案 0 :(得分:1)

本教程是为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问题。