This code is in django / db / models / fields.py Does it create / define an exception?
class ReverseSingleRelatedObjectDescriptor(six.with_metaclass(RenameRelatedObjectDescriptorMethods)):
This is in django / db / models / fields / related.py, it raises the above exception:
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)
The problem is that this code:
try: val = getattr(obj, attr_name) except related.ReverseSingleRelatedObjectDescriptor.RelatedObjectDoesNotExist: val = None
isinstance (foo, related.FieldDoesNotExist) False except Exception as foo: Print type (foo) # Captures here, not higher
won't catch this exception
>>>print type(foo) <class 'django.db.models.fields.related.RelatedObjectDoesNotExist'>
and
except related.RelatedObjectDoesNotExist:
Raises 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
probably that's why.
python django exception
boatcoder Oct 09 2018-11-14T00: 00Z
source share