I am accustomed to KeyError giving me useful information. If you get KeyError("foobar"), you know that "foobar" was not found. But in Django, when you do User.objects.get(username="simon") for example, you get back User.DoesNotExist("User matching query does not exist."). There is no information in the exception’s args that I can’t already infer based on the type of the exception. Why must it be this way?
I made myself a wrapper function:
def get_object(model, **kwargs):
try:
return model.objects.get(**kwargs)
except model.DoesNotExist as e:
e.args = [str(kwargs)]
raise
With this wrapper function you get back User.DoesNotExist("{'username': 'simon'}"). Is there a reason this should not be the default Django experience?