Code

Ticket #13400: 13400-better-doesnotexist-error-message.diff

File 13400-better-doesnotexist-error-message.diff, 835 bytes (added by aaron, 4 years ago)

Patch to give better error message when QuerySet.get(...) raises model.DoesNotExist

Line 
1diff --git a/django/db/models/query.py b/django/db/models/query.py
2index 2bcaf98..96a1c2b 100644
3--- a/django/db/models/query.py
4+++ b/django/db/models/query.py
5@@ -338,8 +338,11 @@ class QuerySet(object):
6         if num == 1:
7             return clone._result_cache[0]
8         if not num:
9-            raise self.model.DoesNotExist("%s matching query does not exist."
10-                    % self.model._meta.object_name)
11+            raise self.model.DoesNotExist(
12+                "%s matching query does not exist.  "
13+                "get() parameters were %s"
14+                % (self.model._meta.object_name, kwargs)
15+            )
16         raise self.model.MultipleObjectsReturned("get() returned more than one %s -- it returned %s! Lookup parameters were %s"
17                 % (self.model._meta.object_name, num, kwargs))
18