Django: Where does “DoesNotExist” come from?

DoesNotExist is documented here:

The DoesNotExist exception inherits
from
django.core.exceptions.ObjectDoesNotExist,
so you can target multiple
DoesNotExist exceptions.

so you can perfectly well use except ObjectDoesNotExist: and catch all the model-specific DoesNotExist exceptions that might be raised in the try clause, or use except SomeSpecificModel.DoesNotExist: when you want to be more specific.

If you’re looking for the specific spot in Django’s source code where this attribute is added to model classes, see here, lines 34-37:

# Create the class.
new_class = type.__new__(cls, name, bases, {'__module__': attrs.pop('__module__')})
new_class.add_to_class('_meta', Options(attrs.pop('Meta', None)))
new_class.add_to_class('DoesNotExist', types.ClassType('DoesNotExist', (ObjectDoesNotExist,), {}))

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)