你怎么抓住这个例外?

boa*_*der 146 python django exception

这段代码在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),
            {}
        )
Run Code Online (Sandbox Code Playgroud)

这是在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
Run Code Online (Sandbox Code Playgroud)

问题是这段代码:

    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
Run Code Online (Sandbox Code Playgroud)

不会抓住那个例外

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

except related.RelatedObjectDoesNotExist:
Run Code Online (Sandbox Code Playgroud)

提高了 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
Run Code Online (Sandbox Code Playgroud)

这可能就是原因.

tde*_*ney 276

如果您的相关模型名为Foo,您可以这样做:

except Foo.DoesNotExist:
Run Code Online (Sandbox Code Playgroud)

当它不可怕时,Django是惊人的.RelatedObjectDoesNotExist是一个返回在运行时动态计算出来的类型的属性.该类型self.field.rel.to.DoesNotExist用作基类.根据Django文档:

ObjectDoesNotExist和DoesNotExist

异常DoesNotExist

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

这就是实现这一目标的神奇之处.建立模型后,该模型是否self.field.rel.to.DoesNotExist存在不存在的异常.

  • 因为这个bug存在于DjangoRestFramework中,并且该模型在某些时候很难获得.我选择了捕获ObjectDoesNotExist. (7认同)
  • 您也可以使用AttributeError,在某些情况下它可能是更好的选项(当您访问记录的"属性"时,几乎总会发生此错误,因此您无需跟踪此属性是否对应于记录与否. (3认同)
  • 是啊啊好吧。但为什么它不能直接返回 None 呢?特别是对于一对一的字段。或者有什么充分的理由吗? (2认同)

Fus*_*ush 49

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

except MyModel.related_field.RelatedObjectDoesNotExist:
Run Code Online (Sandbox Code Playgroud)

要么

except my_model_instance._meta.model.related_field.RelatedObjectDoesNotExist:
Run Code Online (Sandbox Code Playgroud)

related_field字段名称在哪里.

  • 这实际上非常有用,以防您需要避免循环导入.谢谢 (6认同)

Zag*_*ags 29

要抓住这个例外,你可以做到

from django.core.exceptions import ObjectDoesNotExist

try:
    # Your code here
except ObjectDoesNotExist:
    # Handle exception
Run Code Online (Sandbox Code Playgroud)

  • 我发现这实际上并没有按预期捕获错误。`&lt;Model&gt;.DoesNotExist` 做了 (2认同)

C S*_*C S 9

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),
        {}
    )
Run Code Online (Sandbox Code Playgroud)

所以异常继承自<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'>]
Run Code Online (Sandbox Code Playgroud)

基本的要点是你可以捕捉<model name>.DoesNotExist,ObjectDoesNotExist(从中导入django.core.exceptions)或者AttributeError在你的上下文中最有意义的东西.


Muh*_*eed 5

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

2种方法来处理这个。

第一:

当我们需要捕获异常时

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

第二: 当不想处理异常时

>>> hasattr(p2, 'restaurant')
False
Run Code Online (Sandbox Code Playgroud)