All the time in Django I see DoesNotExist being raised like in db.models.fields.related.py. Not ObjectDoesNotExist which is defined in django.core.exceptions, but just DoesNotExist. Where is this exception class defined, or am I not fully understanding exceptions? I’ve checked that it’s not in exceptions (at least not that I know of). I’m confused obviously.
Note: It also comes free, as an attribute of a model sub-class instance, like `self.someforeignkey.DoesNotExist. How is this possible?
DoesNotExistis documented here:so you can perfectly well use
except ObjectDoesNotExist:and catch all the model-specificDoesNotExistexceptions that might be raised in thetryclause, or useexcept 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: