您如何捕获此异常?


161

这段代码在django / db / models / fields.py中。它创建/定义一个异常吗?

class ReverseSingleRelatedObjectDescriptor(six.with_metaclass(RenameRelatedObjectDescriptorMethods)):
    # This class provides the functionality that makes the related-object
    # managers available as attributes on a model class, for fields that have
    # a single "remote" value, on the class that defines the related field.
    # In the example "choice.poll", the poll attribute is a
    # ReverseSingleRelatedObjectDescriptor instance.
    def __init__(self, field_with_rel):
        self.field = field_with_rel
        self.cache_name = self.field.get_cache_name()

    @cached_property
    def RelatedObjectDoesNotExist(self):
        # The exception can't be created at initialization time since the
        # related model might not be resolved yet; `rel.to` might still be
        # a string model reference.
        return type(
            str('RelatedObjectDoesNotExist'),
            (self.field.rel.to.DoesNotExist, AttributeError),
            {}
        )

这在django / db / models / fields / related.py中,它在上面引发了上述异常:

def __get__(self, instance, instance_type=None):
    if instance is None:
        return self
    try:
        rel_obj = getattr(instance, self.cache_name)
    except AttributeError:
        val = self.field.get_local_related_value(instance)
        if None in val:
            rel_obj = None
        else:
            params = dict(
                (rh_field.attname, getattr(instance, lh_field.attname))
                for lh_field, rh_field in self.field.related_fields)
            qs = self.get_queryset(instance=instance)
            extra_filter = self.field.get_extra_descriptor_filter(instance)
            if isinstance(extra_filter, dict):
                params.update(extra_filter)
                qs = qs.filter(**params)
            else:
                qs = qs.filter(extra_filter, **params)
            # Assuming the database enforces foreign keys, this won't fail.
            rel_obj = qs.get()
            if not self.field.rel.multiple:
                setattr(rel_obj, self.field.related.get_cache_name(), instance)
        setattr(instance, self.cache_name, rel_obj)
    if rel_obj is None and not self.field.null:
        raise self.RelatedObjectDoesNotExist(
            "%s has no %s." % (self.field.model.__name__, self.field.name)
        )
    else:
        return rel_obj

问题是此代码:

    try:
        val = getattr(obj, attr_name)
    except related.ReverseSingleRelatedObjectDescriptor.RelatedObjectDoesNotExist:
        val = None  # Does not catch the thrown exception
    except Exception as foo:
        print type(foo)  # Catches here, not above

不会捕获该异常

>>>print type(foo)
<class 'django.db.models.fields.related.RelatedObjectDoesNotExist'>
>>>isinstance(foo, related.FieldDoesNotExist)
False

except related.RelatedObjectDoesNotExist:

提出一个 AttributeError: 'module' object has no attribute 'RelatedObjectDoesNotExist'

>>>isinstance(foo, related.ReverseSingleRelatedObjectDescriptor.RelatedObjectDoesNotExist)
Traceback (most recent call last):
  File "<string>", line 1, in <fragment>
TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types

这可能是为什么。


你怎么进口的related
约翰·兹温克

4
使用AttributeError代替related.ReverseSingleRelatedObjectDescriptor.RelatedObjectDoesNotExist
凯瑟琳

是的,我已经导入了@JohnZwinck。
boatcoder

Answers:


301

如果您的相关模型称为Foo,则可以执行以下操作:

except Foo.DoesNotExist:

Django令人惊叹,但并不可怕。RelatedObjectDoesNotExist是一个属性,该属性返回在运行时动态确定的类型。该类型self.field.rel.to.DoesNotExist用作基类。根据Django文档:

对象不存在和不存在

异常DowsNotExist

DoesNotExist时未找到查询的给定参数的对象异常。Django提供了DidNotExist 异常作为每个模型类的属性,以标识找不到的对象类,并允许您使用try/except捕获特定的模型类。

这就是使之成为现实的魔力。一旦建立了模型,该模型self.field.rel.to.DoesNotExist就是不存在的异常。


7
因为该错误存在于DjangoRestFramework中,所以此时很难建立模型。我决定赶上ObjectDoesNotExist。
boatcoder 2014年

3
您还可以使用AttributeError,这在某些情况下可能是一个更好的选择(当您访问记录的“属性”时,几乎总是会发生此错误,因此,您不必跟踪此属性是否对应于某个属性记录与否
乔丹·瑞特

1
是的,好的。但是为什么不能只返回None?尤其是一对一字段。还是有充分的理由?
尼尔

61

如果您不想导入相关的模型类,则可以:

except MyModel.related_field.RelatedObjectDoesNotExist:

要么

except my_model_instance._meta.model.related_field.RelatedObjectDoesNotExist:

哪里 related_field字段名称。


7
如果您需要避免循环导入,这实际上非常有用。谢谢
Giovanni Di Milia

40

要捕获此异常,通常可以

from django.core.exceptions import ObjectDoesNotExist

try:
    # Your code here
except ObjectDoesNotExist:
    # Handle exception

2
我发现这实际上并没有捕获到预期的错误。在<Model>.DoesNotExist做了
埃里克·布鲁姆

1
@EricBlum <Model> .DoesNotExist是ObjectDoesNotExist的后代,因此不应发生。您能否深入了解发生这种情况的原因或提供有关代码的更多详细信息?
Zags

10

RelatedObjectDoesNotExist在运行时动态创建的例外。以下是ForwardManyToOneDescriptorReverseOneToOneDescriptor描述符的相关代码段:

@cached_property
def RelatedObjectDoesNotExist(self):
    # The exception can't be created at initialization time since the
    # related model might not be resolved yet; `self.field.model` might
    # still be a string model reference.
    return type(
        'RelatedObjectDoesNotExist',
        (self.field.remote_field.model.DoesNotExist, AttributeError),
        {}
    )

因此,异常继承自<model name>.DoesNotExistAttributeError。实际上,此异常类型的完整MRO为:

[<class 'django.db.models.fields.related_descriptors.RelatedObjectDoesNotExist'>, 
<class '<model module path>.DoesNotExist'>,
<class 'django.core.exceptions.ObjectDoesNotExist'>,
<class 'AttributeError'>,
<class 'Exception'>,
<class 'BaseException'>,
<class 'object'>]

基本的要点是您可以捕获<model name>.DoesNotExistObjectDoesNotExist(从导入django.core.exceptions)或AttributeError,在您的上下文中最有意义的。


2

tdelaney的答案非常适合常规代码路径,但是如果您需要知道如何在测试中捕获此异常,则:

from django.core.exceptions import ObjectDoesNotExist

...

    def testCompanyRequired(self):
        with self.assertRaises(ObjectDoesNotExist):
            employee = Employee.objects.create()

2

有点晚了,但对其他人有帮助。

有两种处理方法。

第一:

当我们需要捕获异常时

>>> from django.core.exceptions import ObjectDoesNotExist
>>> try:
>>>     p2.restaurant
>>> except ObjectDoesNotExist:
>>>     print("There is no restaurant here.")
There is no restaurant here.

第二: 当不想处理异常时

>>> hasattr(p2, 'restaurant')
False
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.