How do you catch this exception?

How do you catch this exception?

This code is in django/db/models/  It creates/defines an exception?
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()

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

This is in django/db/models/fields/ it raises the said exception above:
def __get__(self, instance, instance_type=None):
    if instance is None:
        return self
        rel_obj = getattr(instance, self.cache_name)
    except AttributeError:
        val = self.field.get_local_related_value(instance)
        if None in val:
            rel_obj = None
            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):
                qs = qs.filter(**params)
                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__,
        return rel_obj

The problem is that this code:
        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

won't catch that exception
>>>print type(foo)

>>>isinstance(foo, related.FieldDoesNotExist)

except related.RelatedObjectDoesNotExist:

Raises an AttributeError: 'module' object has no attribute 'RelatedObjectDoesNotExist'
>>>isinstance(foo, related.ReverseSingleRelatedObjectDescriptor.RelatedObjectDoesNotExist)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types

which is probably why.


Answer 1:

If your related model is called Foo you can just do:

except Foo.DoesNotExist:

Django is amazing when its not terrifying. RelatedObjectDoesNotExist is a property that returns a type that is figured out dynamically at runtime. That type uses as a base class. According to Django documentation:

ObjectDoesNotExist and DoesNotExist

exception DoesNotExist

The DoesNotExist exception is raised when an object is not found for
the given parameters of a query. Django provides a DoesNotExist
exception as an attribute of each model class to identify the class of
object that could not be found and to allow you to catch a particular
model class with try/except.

This is the magic that makes that happen. Once the model has been built up, is the does-not-exist exception for that model.

Answer 2:

If you don’t want to import the related model class, you can:

except MyModel.related_field.RelatedObjectDoesNotExist:


except my_model_instance._meta.model.related_field.RelatedObjectDoesNotExist:

where related_field is the field name.

Answer 3:

To catch this exception in general, you can do

from django.core.exceptions import ObjectDoesNotExist

    # Your code here
except ObjectDoesNotExist:
    # Handle exception

Answer 4:

The RelatedObjectDoesNotExist exception is created dynamically at runtime. Here is the relevant code snippet for the ForwardManyToOneDescriptor and ReverseOneToOneDescriptor descriptors:

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(
        (self.field.remote_field.model.DoesNotExist, AttributeError),

So the exception inherits from <model name>.DoesNotExist and AttributeError. In fact, the complete MRO for this exception type is:

[<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'>]

The basic takeaway is you can catch <model name>.DoesNotExist, ObjectDoesNotExist (import from django.core.exceptions) or AttributeError, whatever makes the most sense in your context.

Answer 5:

tdelaney’s answer is great for regular code paths, but if you need to know how to catch this exception in tests:

from django.core.exceptions import ObjectDoesNotExist


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