In Django REST Framework, when you query a database model and it does not exist an exception will be raised as ModelName.DoesNotExist
.
This exception will change according to the model name.
For example:
- Querying the
Car
model will raiseCar.DoesNotExist
- Querying the
Plane
model will raisePlane.DoesNotExist
This causes trouble, since you can not catch the exception at one common place, because you do not know the parent class of the Exception.
You have to catch a different exception every time you query a different model, for example:
try:
return Car.objects.get(pk=1)
except Car.DoesNotExist:
raise Http404
Why was this feature designed like this?
Is it possible to capture a common exception?