Ticket #13400: 13400-alt-doesnotexist-error-message.diff
File 13400-alt-doesnotexist-error-message.diff, 10.9 KB (added by , 15 years ago) |
---|
-
django/db/models/query.py
330 330 Performs the query and returns a single object matching the given 331 331 keyword arguments. 332 332 """ 333 333 clone = self.filter(*args, **kwargs) 334 334 if self.query.can_filter(): 335 335 clone = clone.order_by() 336 336 num = len(clone) 337 337 if num == 1: 338 338 return clone._result_cache[0] 339 339 if not num: 340 raise self.model.DoesNotExist("%s matching query does not exist." 341 % self.model._meta.object_name) 340 q = ', '.join([repr(a) for a in args] + ["%s=%r" % i for i in kwargs.items()]) 341 raise self.model.DoesNotExist(u"%s matching query (`%s`) does not exist." 342 % (self.model._meta.object_name, q)) 342 343 raise self.model.MultipleObjectsReturned("get() returned more than one %s -- it returned %s! Lookup parameters were %s" 343 344 % (self.model._meta.object_name, num, kwargs)) 344 345 345 346 def create(self, **kwargs): 346 347 """ 347 348 Creates a new object with the given kwargs, saving it to the database 348 349 and returning the created object. 349 350 """ 350 351 obj = self.model(**kwargs) 351 352 self._for_write = True -
django/contrib/sites/tests.py
5 5 6 6 # Make sure that get_current() does not return a deleted Site object. 7 7 >>> s = Site.objects.get_current() 8 8 >>> isinstance(s, Site) 9 9 True 10 10 11 11 >>> s.delete() 12 12 >>> Site.objects.get_current() 13 13 Traceback (most recent call last): 14 14 ... 15 DoesNotExist: Site matching query does not exist.15 DoesNotExist: Site matching query (`pk=1`) does not exist. 16 16 17 17 # After updating a Site object (e.g. via the admin), we shouldn't return a 18 18 # bogus value from the SITE_CACHE. 19 19 >>> _ = Site.objects.create(id=settings.SITE_ID, domain="example.com", name="example.com") 20 20 >>> site = Site.objects.get_current() 21 21 >>> site.name 22 22 u"example.com" 23 23 >>> s2 = Site.objects.get(id=settings.SITE_ID) 24 24 >>> s2.name = "Example site" 25 25 >>> s2.save() -
tests/modeltests/custom_pk/models.py
51 51 >>> Employee.objects.all() 52 52 [<Employee: Fran Bones>, <Employee: Dan Jones>] 53 53 54 54 >>> Employee.objects.get(pk=123) 55 55 <Employee: Dan Jones> 56 56 >>> Employee.objects.get(pk=456) 57 57 <Employee: Fran Bones> 58 58 >>> Employee.objects.get(pk=42) 59 59 Traceback (most recent call last): 60 60 ... 61 DoesNotExist: Employee matching query does not exist.61 DoesNotExist: Employee matching query (`pk=42`) does not exist. 62 62 63 63 # Use the name of the primary key, rather than pk. 64 64 >>> Employee.objects.get(employee_code__exact=123) 65 65 <Employee: Dan Jones> 66 66 67 67 # pk can be used as a substitute for the primary key. 68 68 >>> Employee.objects.filter(pk__in=[123, 456]) 69 69 [<Employee: Fran Bones>, <Employee: Dan Jones>] 70 70 71 71 # The primary key can be accessed via the pk property on the model. -
tests/modeltests/model_inheritance/models.py
287 287 >>> Place.objects.get(name='Ristorante Miron').restaurant.italianrestaurant 288 288 <ItalianRestaurant: Ristorante Miron the italian restaurant> 289 289 >>> Restaurant.objects.get(name='Ristorante Miron').italianrestaurant 290 290 <ItalianRestaurant: Ristorante Miron the italian restaurant> 291 291 292 292 # This won't work because the Demon Dogs restaurant is not an Italian 293 293 # restaurant. 294 294 >>> place.restaurant.italianrestaurant 295 295 Traceback (most recent call last): 296 296 ... 297 DoesNotExist: ItalianRestaurant matching query does not exist.297 DoesNotExist: ItalianRestaurant matching query (`restaurant_ptr__pk=3`) does not exist. 298 298 299 299 # An ItalianRestaurant which does not exist is also a Place which does not exist. 300 300 >>> try: 301 301 ... ItalianRestaurant.objects.get(name='The Noodle Void') 302 302 ... except Place.DoesNotExist: 303 303 ... pass 304 304 305 305 # MultipleObjectsReturned is also inherited. 306 306 >>> try: 307 307 ... Restaurant.objects.get(id__lt=10) … … 316 316 >>> s2 = Supplier(name="Luigi's Pasta", address='456 Sesame St') 317 317 >>> s2.save() 318 318 >>> s2.customers = [ir] 319 319 320 320 # This won't work because the Place we select is not a Restaurant (it's a 321 321 # Supplier). 322 322 >>> p = Place.objects.get(name="Joe's Chickens") 323 323 >>> p.restaurant 324 324 Traceback (most recent call last): 325 325 ... 326 DoesNotExist: Restaurant matching query does not exist.326 DoesNotExist: Restaurant matching query (`place_ptr__pk=5`) does not exist. 327 327 328 328 # But we can descend from p to the Supplier child, as expected. 329 329 >>> p.supplier 330 330 <Supplier: Joe's Chickens the supplier> 331 331 332 332 >>> ir.provider.order_by('-name') 333 333 [<Supplier: Luigi's Pasta the supplier>, <Supplier: Joe's Chickens the supplier>] 334 334 335 335 >>> Restaurant.objects.filter(provider__name__contains="Chickens") 336 336 [<Restaurant: Ristorante Miron the restaurant>, <Restaurant: Demon Dogs the restaurant>] -
tests/modeltests/basic/models.py
93 93 >>> Article.objects.filter(pub_date__week_day=5) 94 94 [<Article: Area woman programs in Python>] 95 95 >>> Article.objects.filter(pub_date__week_day=6) 96 96 [] 97 97 98 98 # Django raises an Article.DoesNotExist exception for get() if the parameters 99 99 # don't match any object. 100 100 >>> Article.objects.get(id__exact=2) 101 101 Traceback (most recent call last): 102 102 ... 103 DoesNotExist: Article matching query does not exist.103 DoesNotExist: Article matching query (`id__exact=2`) does not exist. 104 104 105 105 >>> Article.objects.get(pub_date__year=2005, pub_date__month=8) 106 106 Traceback (most recent call last): 107 107 ... 108 DoesNotExist: Article matching query does not exist.108 DoesNotExist: Article matching query (`pub_date__year=2005, pub_date__month=8`) does not exist. 109 109 110 110 >>> Article.objects.get(pub_date__week_day=6) 111 111 Traceback (most recent call last): 112 112 ... 113 DoesNotExist: Article matching query does not exist.113 DoesNotExist: Article matching query (`pub_date__week_day=6`) does not exist. 114 114 115 115 # Lookup by a primary key is the most common case, so Django provides a 116 116 # shortcut for primary-key exact lookups. 117 117 # The following is identical to articles.get(id=1). 118 118 >>> Article.objects.get(pk=1) 119 119 <Article: Area woman programs in Python> 120 120 121 121 # pk can be used as a shortcut for the primary key name in any query 122 122 >>> Article.objects.filter(pk__in=[1]) 123 123 [<Article: Area woman programs in Python>] -
tests/modeltests/one_to_one/models.py
62 62 <Place: Demon Dogs the place> 63 63 64 64 # A Place can access its restaurant, if available. 65 65 >>> p1.restaurant 66 66 <Restaurant: Demon Dogs the restaurant> 67 67 68 68 # p2 doesn't have an associated restaurant. 69 69 >>> p2.restaurant 70 70 Traceback (most recent call last): 71 71 ... 72 DoesNotExist: Restaurant matching query does not exist.72 DoesNotExist: Restaurant matching query (`place__pk=2`) does not exist. 73 73 74 74 # Set the place using assignment notation. Because place is the primary key on 75 75 # Restaurant, the save will create a new restaurant 76 76 >>> r.place = p2 77 77 >>> r.save() 78 78 >>> p2.restaurant 79 79 <Restaurant: Ace Hardware the restaurant> 80 80 >>> r.place 81 81 <Place: Ace Hardware the place> 82 82 >>> p2.id -
tests/modeltests/get_latest/models.py
27 27 # Note that this model doesn't have "get_latest_by" set. 28 28 29 29 def __unicode__(self): 30 30 return self.name 31 31 32 32 __test__ = {'API_TESTS':""" 33 33 # Because no Articles exist yet, latest() raises ArticleDoesNotExist. 34 34 >>> Article.objects.latest() 35 35 Traceback (most recent call last): 36 36 ... 37 DoesNotExist: Article matching query does not exist.37 DoesNotExist: Article matching query (``) does not exist. 38 38 39 39 # Create a couple of Articles. 40 40 >>> from datetime import datetime 41 41 >>> a1 = Article(headline='Article 1', pub_date=datetime(2005, 7, 26), expire_date=datetime(2005, 9, 1)) 42 42 >>> a1.save() 43 43 >>> a2 = Article(headline='Article 2', pub_date=datetime(2005, 7, 27), expire_date=datetime(2005, 7, 28)) 44 44 >>> a2.save() 45 45 >>> a3 = Article(headline='Article 3', pub_date=datetime(2005, 7, 27), expire_date=datetime(2005, 8, 27)) 46 46 >>> a3.save() 47 47 >>> a4 = Article(headline='Article 4', pub_date=datetime(2005, 7, 28), expire_date=datetime(2005, 7, 30)) -
tests/regressiontests/model_inheritance_regress/models.py
239 239 >>> Place.objects.get(pk=ident) 240 240 <Place: Guido's All New House of Pasta the place> 241 241 >>> xx = Restaurant.objects.create(name='a', address='xx', serves_hot_dogs=True, serves_pizza=False) 242 242 243 243 # This should delete both Restuarants, plus the related places, plus the ItalianRestaurant. 244 244 >>> Restaurant.objects.all().delete() 245 245 246 246 >>> Place.objects.get(pk=ident) 247 247 Traceback (most recent call last): 248 248 ... 249 DoesNotExist: Place matching query does not exist.249 DoesNotExist: Place matching query (`pk=1`) does not exist. 250 250 251 251 >>> ItalianRestaurant.objects.get(pk=ident) 252 252 Traceback (most recent call last): 253 253 ... 254 DoesNotExist: ItalianRestaurant matching query does not exist.254 DoesNotExist: ItalianRestaurant matching query (`pk=1`) does not exist. 255 255 256 256 # Regression test for #6755 257 257 >>> r = Restaurant(serves_pizza=False) 258 258 >>> r.save() 259 259 >>> r.id == r.place_ptr_id 260 260 True 261 261 >>> orig_id = r.id 262 262 >>> r = Restaurant(place_ptr_id=orig_id, serves_pizza=True) 263 263 >>> r.save() 264 264 >>> r.id == orig_id -
tests/regressiontests/reverse_single_related/models.py
42 42 43 43 # If the manager is marked "use_for_related_fields", it'll get used instead 44 44 # of the "bare" queryset. Usually you'd define this as a property on the class, 45 45 # but this approximates that in a way that's easier in tests. 46 46 47 47 >>> Source.objects.use_for_related_fields = True 48 48 >>> private_item = Item.objects.get(pk=private_item.pk) 49 49 >>> private_item.source 50 50 Traceback (most recent call last): 51 51 ... 52 DoesNotExist: Source matching query does not exist.52 DoesNotExist: Source matching query (`id__exact=2`) does not exist. 53 53 54 54 """}