Code

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

File 13400-alt-doesnotexist-error-message.diff, 10.9 KB (added by trentm, 4 years ago)

My alternative improved DoesNotExist error message patch (with updates for tests)

Line 
1Index: django/db/models/query.py
2===================================================================
3--- django/db/models/query.py   (revision 13036)
4+++ django/db/models/query.py   (working copy)
5@@ -330,22 +330,23 @@
6         Performs the query and returns a single object matching the given
7         keyword arguments.
8         """
9         clone = self.filter(*args, **kwargs)
10         if self.query.can_filter():
11             clone = clone.order_by()
12         num = len(clone)
13         if num == 1:
14             return clone._result_cache[0]
15         if not num:
16-            raise self.model.DoesNotExist("%s matching query does not exist."
17-                    % self.model._meta.object_name)
18+            q = ', '.join([repr(a) for a in args] + ["%s=%r" % i for i in kwargs.items()])
19+            raise self.model.DoesNotExist(u"%s matching query (`%s`) does not exist."
20+                    % (self.model._meta.object_name, q))
21         raise self.model.MultipleObjectsReturned("get() returned more than one %s -- it returned %s! Lookup parameters were %s"
22                 % (self.model._meta.object_name, num, kwargs))
23 
24     def create(self, **kwargs):
25         """
26         Creates a new object with the given kwargs, saving it to the database
27         and returning the created object.
28         """
29         obj = self.model(**kwargs)
30         self._for_write = True
31Index: django/contrib/sites/tests.py
32===================================================================
33--- django/contrib/sites/tests.py       (revision 13036)
34+++ django/contrib/sites/tests.py       (working copy)
35@@ -5,21 +5,21 @@
36 
37 # Make sure that get_current() does not return a deleted Site object.
38 >>> s = Site.objects.get_current()
39 >>> isinstance(s, Site)
40 True
41 
42 >>> s.delete()
43 >>> Site.objects.get_current()
44 Traceback (most recent call last):
45 ...
46-DoesNotExist: Site matching query does not exist.
47+DoesNotExist: Site matching query (`pk=1`) does not exist.
48 
49 # After updating a Site object (e.g. via the admin), we shouldn't return a
50 # bogus value from the SITE_CACHE.
51 >>> _ = Site.objects.create(id=settings.SITE_ID, domain="example.com", name="example.com")
52 >>> site = Site.objects.get_current()
53 >>> site.name
54 u"example.com"
55 >>> s2 = Site.objects.get(id=settings.SITE_ID)
56 >>> s2.name = "Example site"
57 >>> s2.save()
58Index: tests/modeltests/custom_pk/models.py
59===================================================================
60--- tests/modeltests/custom_pk/models.py        (revision 13036)
61+++ tests/modeltests/custom_pk/models.py        (working copy)
62@@ -51,21 +51,21 @@
63 >>> Employee.objects.all()
64 [<Employee: Fran Bones>, <Employee: Dan Jones>]
65 
66 >>> Employee.objects.get(pk=123)
67 <Employee: Dan Jones>
68 >>> Employee.objects.get(pk=456)
69 <Employee: Fran Bones>
70 >>> Employee.objects.get(pk=42)
71 Traceback (most recent call last):
72     ...
73-DoesNotExist: Employee matching query does not exist.
74+DoesNotExist: Employee matching query (`pk=42`) does not exist.
75 
76 # Use the name of the primary key, rather than pk.
77 >>> Employee.objects.get(employee_code__exact=123)
78 <Employee: Dan Jones>
79 
80 # pk can be used as a substitute for the primary key.
81 >>> Employee.objects.filter(pk__in=[123, 456])
82 [<Employee: Fran Bones>, <Employee: Dan Jones>]
83 
84 # The primary key can be accessed via the pk property on the model.
85Index: tests/modeltests/model_inheritance/models.py
86===================================================================
87--- tests/modeltests/model_inheritance/models.py        (revision 13036)
88+++ tests/modeltests/model_inheritance/models.py        (working copy)
89@@ -287,21 +287,21 @@
90 >>> Place.objects.get(name='Ristorante Miron').restaurant.italianrestaurant
91 <ItalianRestaurant: Ristorante Miron the italian restaurant>
92 >>> Restaurant.objects.get(name='Ristorante Miron').italianrestaurant
93 <ItalianRestaurant: Ristorante Miron the italian restaurant>
94 
95 # This won't work because the Demon Dogs restaurant is not an Italian
96 # restaurant.
97 >>> place.restaurant.italianrestaurant
98 Traceback (most recent call last):
99     ...
100-DoesNotExist: ItalianRestaurant matching query does not exist.
101+DoesNotExist: ItalianRestaurant matching query (`restaurant_ptr__pk=3`) does not exist.
102 
103 # An ItalianRestaurant which does not exist is also a Place which does not exist.
104 >>> try:
105 ...     ItalianRestaurant.objects.get(name='The Noodle Void')
106 ... except Place.DoesNotExist:
107 ...     pass
108 
109 # MultipleObjectsReturned is also inherited.
110 >>> try:
111 ...     Restaurant.objects.get(id__lt=10)
112@@ -316,21 +316,21 @@
113 >>> s2 = Supplier(name="Luigi's Pasta", address='456 Sesame St')
114 >>> s2.save()
115 >>> s2.customers = [ir]
116 
117 # This won't work because the Place we select is not a Restaurant (it's a
118 # Supplier).
119 >>> p = Place.objects.get(name="Joe's Chickens")
120 >>> p.restaurant
121 Traceback (most recent call last):
122     ...
123-DoesNotExist: Restaurant matching query does not exist.
124+DoesNotExist: Restaurant matching query (`place_ptr__pk=5`) does not exist.
125 
126 # But we can descend from p to the Supplier child, as expected.
127 >>> p.supplier
128 <Supplier: Joe's Chickens the supplier>
129 
130 >>> ir.provider.order_by('-name')
131 [<Supplier: Luigi's Pasta the supplier>, <Supplier: Joe's Chickens the supplier>]
132 
133 >>> Restaurant.objects.filter(provider__name__contains="Chickens")
134 [<Restaurant: Ristorante Miron the restaurant>, <Restaurant: Demon Dogs the restaurant>]
135Index: tests/modeltests/basic/models.py
136===================================================================
137--- tests/modeltests/basic/models.py    (revision 13036)
138+++ tests/modeltests/basic/models.py    (working copy)
139@@ -93,31 +93,31 @@
140 >>> Article.objects.filter(pub_date__week_day=5)
141 [<Article: Area woman programs in Python>]
142 >>> Article.objects.filter(pub_date__week_day=6)
143 []
144 
145 # Django raises an Article.DoesNotExist exception for get() if the parameters
146 # don't match any object.
147 >>> Article.objects.get(id__exact=2)
148 Traceback (most recent call last):
149     ...
150-DoesNotExist: Article matching query does not exist.
151+DoesNotExist: Article matching query (`id__exact=2`) does not exist.
152 
153 >>> Article.objects.get(pub_date__year=2005, pub_date__month=8)
154 Traceback (most recent call last):
155     ...
156-DoesNotExist: Article matching query does not exist.
157+DoesNotExist: Article matching query (`pub_date__year=2005, pub_date__month=8`) does not exist.
158 
159 >>> Article.objects.get(pub_date__week_day=6)
160 Traceback (most recent call last):
161     ...
162-DoesNotExist: Article matching query does not exist.
163+DoesNotExist: Article matching query (`pub_date__week_day=6`) does not exist.
164 
165 # Lookup by a primary key is the most common case, so Django provides a
166 # shortcut for primary-key exact lookups.
167 # The following is identical to articles.get(id=1).
168 >>> Article.objects.get(pk=1)
169 <Article: Area woman programs in Python>
170 
171 # pk can be used as a shortcut for the primary key name in any query
172 >>> Article.objects.filter(pk__in=[1])
173 [<Article: Area woman programs in Python>]
174Index: tests/modeltests/one_to_one/models.py
175===================================================================
176--- tests/modeltests/one_to_one/models.py       (revision 13036)
177+++ tests/modeltests/one_to_one/models.py       (working copy)
178@@ -62,21 +62,21 @@
179 <Place: Demon Dogs the place>
180 
181 # A Place can access its restaurant, if available.
182 >>> p1.restaurant
183 <Restaurant: Demon Dogs the restaurant>
184 
185 # p2 doesn't have an associated restaurant.
186 >>> p2.restaurant
187 Traceback (most recent call last):
188     ...
189-DoesNotExist: Restaurant matching query does not exist.
190+DoesNotExist: Restaurant matching query (`place__pk=2`) does not exist.
191 
192 # Set the place using assignment notation. Because place is the primary key on
193 # Restaurant, the save will create a new restaurant
194 >>> r.place = p2
195 >>> r.save()
196 >>> p2.restaurant
197 <Restaurant: Ace Hardware the restaurant>
198 >>> r.place
199 <Place: Ace Hardware the place>
200 >>> p2.id
201Index: tests/modeltests/get_latest/models.py
202===================================================================
203--- tests/modeltests/get_latest/models.py       (revision 13036)
204+++ tests/modeltests/get_latest/models.py       (working copy)
205@@ -27,21 +27,21 @@
206     # Note that this model doesn't have "get_latest_by" set.
207 
208     def __unicode__(self):
209         return self.name
210 
211 __test__ = {'API_TESTS':"""
212 # Because no Articles exist yet, latest() raises ArticleDoesNotExist.
213 >>> Article.objects.latest()
214 Traceback (most recent call last):
215     ...
216-DoesNotExist: Article matching query does not exist.
217+DoesNotExist: Article matching query (``) does not exist.
218 
219 # Create a couple of Articles.
220 >>> from datetime import datetime
221 >>> a1 = Article(headline='Article 1', pub_date=datetime(2005, 7, 26), expire_date=datetime(2005, 9, 1))
222 >>> a1.save()
223 >>> a2 = Article(headline='Article 2', pub_date=datetime(2005, 7, 27), expire_date=datetime(2005, 7, 28))
224 >>> a2.save()
225 >>> a3 = Article(headline='Article 3', pub_date=datetime(2005, 7, 27), expire_date=datetime(2005, 8, 27))
226 >>> a3.save()
227 >>> a4 = Article(headline='Article 4', pub_date=datetime(2005, 7, 28), expire_date=datetime(2005, 7, 30))
228Index: tests/regressiontests/model_inheritance_regress/models.py
229===================================================================
230--- tests/regressiontests/model_inheritance_regress/models.py   (revision 13036)
231+++ tests/regressiontests/model_inheritance_regress/models.py   (working copy)
232@@ -239,26 +239,26 @@
233 >>> Place.objects.get(pk=ident)
234 <Place: Guido's All New House of Pasta the place>
235 >>> xx = Restaurant.objects.create(name='a', address='xx', serves_hot_dogs=True, serves_pizza=False)
236 
237 # This should delete both Restuarants, plus the related places, plus the ItalianRestaurant.
238 >>> Restaurant.objects.all().delete()
239 
240 >>> Place.objects.get(pk=ident)
241 Traceback (most recent call last):
242 ...
243-DoesNotExist: Place matching query does not exist.
244+DoesNotExist: Place matching query (`pk=1`) does not exist.
245 
246 >>> ItalianRestaurant.objects.get(pk=ident)
247 Traceback (most recent call last):
248 ...
249-DoesNotExist: ItalianRestaurant matching query does not exist.
250+DoesNotExist: ItalianRestaurant matching query (`pk=1`) does not exist.
251 
252 # Regression test for #6755
253 >>> r = Restaurant(serves_pizza=False)
254 >>> r.save()
255 >>> r.id == r.place_ptr_id
256 True
257 >>> orig_id = r.id
258 >>> r = Restaurant(place_ptr_id=orig_id, serves_pizza=True)
259 >>> r.save()
260 >>> r.id == orig_id
261Index: tests/regressiontests/reverse_single_related/models.py
262===================================================================
263--- tests/regressiontests/reverse_single_related/models.py      (revision 13036)
264+++ tests/regressiontests/reverse_single_related/models.py      (working copy)
265@@ -42,13 +42,13 @@
266 
267 # If the manager is marked "use_for_related_fields", it'll get used instead
268 # of the "bare" queryset. Usually you'd define this as a property on the class,
269 # but this approximates that in a way that's easier in tests.
270 
271 >>> Source.objects.use_for_related_fields = True
272 >>> private_item = Item.objects.get(pk=private_item.pk)
273 >>> private_item.source
274 Traceback (most recent call last):
275     ...
276-DoesNotExist: Source matching query does not exist.
277+DoesNotExist: Source matching query (`id__exact=2`) does not exist.
278 
279 """}