Skip to content

Commit d5b93d3

Browse files
committed
Fixed django#10494 -- Added kwargs to QuerySet.get() error message in the case no objects were found.
Thanks brondsem for the report, Szymon Pyzalski for the patch and oinopion for review. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17917 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent a901654 commit d5b93d3

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

django/db/models/query.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,14 @@ def get(self, *args, **kwargs):
363363
if num == 1:
364364
return clone._result_cache[0]
365365
if not num:
366-
raise self.model.DoesNotExist("%s matching query does not exist."
367-
% self.model._meta.object_name)
368-
raise self.model.MultipleObjectsReturned("get() returned more than one %s -- it returned %s! Lookup parameters were %s"
369-
% (self.model._meta.object_name, num, kwargs))
366+
raise self.model.DoesNotExist(
367+
"%s matching query does not exist. "
368+
"Lookup parameters were %s" %
369+
(self.model._meta.object_name, kwargs))
370+
raise self.model.MultipleObjectsReturned(
371+
"get() returned more than one %s -- it returned %s! "
372+
"Lookup parameters were %s" %
373+
(self.model._meta.object_name, num, kwargs))
370374

371375
def create(self, **kwargs):
372376
"""

docs/intro/overview.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ access your data. The API is created on the fly, no code generation necessary::
9393
>>> Reporter.objects.get(id=2)
9494
Traceback (most recent call last):
9595
...
96-
DoesNotExist: Reporter matching query does not exist.
96+
DoesNotExist: Reporter matching query does not exist. Lookup parameters were {'id': 2}
9797

9898
# Create an article.
9999
>>> from datetime import datetime

docs/intro/tutorial01.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ Save these changes and start a new Python interactive shell by running
664664
>>> Poll.objects.get(id=2)
665665
Traceback (most recent call last):
666666
...
667-
DoesNotExist: Poll matching query does not exist.
667+
DoesNotExist: Poll matching query does not exist. Lookup parameters were {'id': 2}
668668

669669
# Lookup by a primary key is the most common case, so Django provides a
670670
# shortcut for primary-key exact lookups.

tests/modeltests/basic/tests.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,22 +83,32 @@ def test_lookup(self):
8383
# parameters don't match any object.
8484
self.assertRaisesRegexp(
8585
ObjectDoesNotExist,
86-
"Article matching query does not exist.",
86+
"Article matching query does not exist. Lookup parameters were "
87+
"{'id__exact': 2000}",
8788
Article.objects.get,
8889
id__exact=2000,
8990
)
90-
91+
# To avoid dict-ordering related errors check only one lookup
92+
# in single assert.
93+
self.assertRaisesRegexp(
94+
ObjectDoesNotExist,
95+
".*'pub_date__year': 2005.*",
96+
Article.objects.get,
97+
pub_date__year=2005,
98+
pub_date__month=8,
99+
)
91100
self.assertRaisesRegexp(
92101
ObjectDoesNotExist,
93-
"Article matching query does not exist.",
102+
".*'pub_date__month': 8.*",
94103
Article.objects.get,
95104
pub_date__year=2005,
96105
pub_date__month=8,
97106
)
98107

99108
self.assertRaisesRegexp(
100109
ObjectDoesNotExist,
101-
"Article matching query does not exist.",
110+
"Article matching query does not exist. Lookup parameters were "
111+
"{'pub_date__week_day': 6}",
102112
Article.objects.get,
103113
pub_date__week_day=6,
104114
)

0 commit comments

Comments
 (0)