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

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

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

  • django/db/models/query.py

     
    330330        Performs the query and returns a single object matching the given
    331331        keyword arguments.
    332332        """
    333333        clone = self.filter(*args, **kwargs)
    334334        if self.query.can_filter():
    335335            clone = clone.order_by()
    336336        num = len(clone)
    337337        if num == 1:
    338338            return clone._result_cache[0]
    339339        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))
    342343        raise self.model.MultipleObjectsReturned("get() returned more than one %s -- it returned %s! Lookup parameters were %s"
    343344                % (self.model._meta.object_name, num, kwargs))
    344345
    345346    def create(self, **kwargs):
    346347        """
    347348        Creates a new object with the given kwargs, saving it to the database
    348349        and returning the created object.
    349350        """
    350351        obj = self.model(**kwargs)
    351352        self._for_write = True
  • django/contrib/sites/tests.py

     
    55
    66# Make sure that get_current() does not return a deleted Site object.
    77>>> s = Site.objects.get_current()
    88>>> isinstance(s, Site)
    99True
    1010
    1111>>> s.delete()
    1212>>> Site.objects.get_current()
    1313Traceback (most recent call last):
    1414...
    15 DoesNotExist: Site matching query does not exist.
     15DoesNotExist: Site matching query (`pk=1`) does not exist.
    1616
    1717# After updating a Site object (e.g. via the admin), we shouldn't return a
    1818# bogus value from the SITE_CACHE.
    1919>>> _ = Site.objects.create(id=settings.SITE_ID, domain="example.com", name="example.com")
    2020>>> site = Site.objects.get_current()
    2121>>> site.name
    2222u"example.com"
    2323>>> s2 = Site.objects.get(id=settings.SITE_ID)
    2424>>> s2.name = "Example site"
    2525>>> s2.save()
  • tests/modeltests/custom_pk/models.py

     
    5151>>> Employee.objects.all()
    5252[<Employee: Fran Bones>, <Employee: Dan Jones>]
    5353
    5454>>> Employee.objects.get(pk=123)
    5555<Employee: Dan Jones>
    5656>>> Employee.objects.get(pk=456)
    5757<Employee: Fran Bones>
    5858>>> Employee.objects.get(pk=42)
    5959Traceback (most recent call last):
    6060    ...
    61 DoesNotExist: Employee matching query does not exist.
     61DoesNotExist: Employee matching query (`pk=42`) does not exist.
    6262
    6363# Use the name of the primary key, rather than pk.
    6464>>> Employee.objects.get(employee_code__exact=123)
    6565<Employee: Dan Jones>
    6666
    6767# pk can be used as a substitute for the primary key.
    6868>>> Employee.objects.filter(pk__in=[123, 456])
    6969[<Employee: Fran Bones>, <Employee: Dan Jones>]
    7070
    7171# The primary key can be accessed via the pk property on the model.
  • tests/modeltests/model_inheritance/models.py

     
    287287>>> Place.objects.get(name='Ristorante Miron').restaurant.italianrestaurant
    288288<ItalianRestaurant: Ristorante Miron the italian restaurant>
    289289>>> Restaurant.objects.get(name='Ristorante Miron').italianrestaurant
    290290<ItalianRestaurant: Ristorante Miron the italian restaurant>
    291291
    292292# This won't work because the Demon Dogs restaurant is not an Italian
    293293# restaurant.
    294294>>> place.restaurant.italianrestaurant
    295295Traceback (most recent call last):
    296296    ...
    297 DoesNotExist: ItalianRestaurant matching query does not exist.
     297DoesNotExist: ItalianRestaurant matching query (`restaurant_ptr__pk=3`) does not exist.
    298298
    299299# An ItalianRestaurant which does not exist is also a Place which does not exist.
    300300>>> try:
    301301...     ItalianRestaurant.objects.get(name='The Noodle Void')
    302302... except Place.DoesNotExist:
    303303...     pass
    304304
    305305# MultipleObjectsReturned is also inherited.
    306306>>> try:
    307307...     Restaurant.objects.get(id__lt=10)
     
    316316>>> s2 = Supplier(name="Luigi's Pasta", address='456 Sesame St')
    317317>>> s2.save()
    318318>>> s2.customers = [ir]
    319319
    320320# This won't work because the Place we select is not a Restaurant (it's a
    321321# Supplier).
    322322>>> p = Place.objects.get(name="Joe's Chickens")
    323323>>> p.restaurant
    324324Traceback (most recent call last):
    325325    ...
    326 DoesNotExist: Restaurant matching query does not exist.
     326DoesNotExist: Restaurant matching query (`place_ptr__pk=5`) does not exist.
    327327
    328328# But we can descend from p to the Supplier child, as expected.
    329329>>> p.supplier
    330330<Supplier: Joe's Chickens the supplier>
    331331
    332332>>> ir.provider.order_by('-name')
    333333[<Supplier: Luigi's Pasta the supplier>, <Supplier: Joe's Chickens the supplier>]
    334334
    335335>>> Restaurant.objects.filter(provider__name__contains="Chickens")
    336336[<Restaurant: Ristorante Miron the restaurant>, <Restaurant: Demon Dogs the restaurant>]
  • tests/modeltests/basic/models.py

     
    9393>>> Article.objects.filter(pub_date__week_day=5)
    9494[<Article: Area woman programs in Python>]
    9595>>> Article.objects.filter(pub_date__week_day=6)
    9696[]
    9797
    9898# Django raises an Article.DoesNotExist exception for get() if the parameters
    9999# don't match any object.
    100100>>> Article.objects.get(id__exact=2)
    101101Traceback (most recent call last):
    102102    ...
    103 DoesNotExist: Article matching query does not exist.
     103DoesNotExist: Article matching query (`id__exact=2`) does not exist.
    104104
    105105>>> Article.objects.get(pub_date__year=2005, pub_date__month=8)
    106106Traceback (most recent call last):
    107107    ...
    108 DoesNotExist: Article matching query does not exist.
     108DoesNotExist: Article matching query (`pub_date__year=2005, pub_date__month=8`) does not exist.
    109109
    110110>>> Article.objects.get(pub_date__week_day=6)
    111111Traceback (most recent call last):
    112112    ...
    113 DoesNotExist: Article matching query does not exist.
     113DoesNotExist: Article matching query (`pub_date__week_day=6`) does not exist.
    114114
    115115# Lookup by a primary key is the most common case, so Django provides a
    116116# shortcut for primary-key exact lookups.
    117117# The following is identical to articles.get(id=1).
    118118>>> Article.objects.get(pk=1)
    119119<Article: Area woman programs in Python>
    120120
    121121# pk can be used as a shortcut for the primary key name in any query
    122122>>> Article.objects.filter(pk__in=[1])
    123123[<Article: Area woman programs in Python>]
  • tests/modeltests/one_to_one/models.py

     
    6262<Place: Demon Dogs the place>
    6363
    6464# A Place can access its restaurant, if available.
    6565>>> p1.restaurant
    6666<Restaurant: Demon Dogs the restaurant>
    6767
    6868# p2 doesn't have an associated restaurant.
    6969>>> p2.restaurant
    7070Traceback (most recent call last):
    7171    ...
    72 DoesNotExist: Restaurant matching query does not exist.
     72DoesNotExist: Restaurant matching query (`place__pk=2`) does not exist.
    7373
    7474# Set the place using assignment notation. Because place is the primary key on
    7575# Restaurant, the save will create a new restaurant
    7676>>> r.place = p2
    7777>>> r.save()
    7878>>> p2.restaurant
    7979<Restaurant: Ace Hardware the restaurant>
    8080>>> r.place
    8181<Place: Ace Hardware the place>
    8282>>> p2.id
  • tests/modeltests/get_latest/models.py

     
    2727    # Note that this model doesn't have "get_latest_by" set.
    2828
    2929    def __unicode__(self):
    3030        return self.name
    3131
    3232__test__ = {'API_TESTS':"""
    3333# Because no Articles exist yet, latest() raises ArticleDoesNotExist.
    3434>>> Article.objects.latest()
    3535Traceback (most recent call last):
    3636    ...
    37 DoesNotExist: Article matching query does not exist.
     37DoesNotExist: Article matching query (``) does not exist.
    3838
    3939# Create a couple of Articles.
    4040>>> from datetime import datetime
    4141>>> a1 = Article(headline='Article 1', pub_date=datetime(2005, 7, 26), expire_date=datetime(2005, 9, 1))
    4242>>> a1.save()
    4343>>> a2 = Article(headline='Article 2', pub_date=datetime(2005, 7, 27), expire_date=datetime(2005, 7, 28))
    4444>>> a2.save()
    4545>>> a3 = Article(headline='Article 3', pub_date=datetime(2005, 7, 27), expire_date=datetime(2005, 8, 27))
    4646>>> a3.save()
    4747>>> a4 = Article(headline='Article 4', pub_date=datetime(2005, 7, 28), expire_date=datetime(2005, 7, 30))
  • tests/regressiontests/model_inheritance_regress/models.py

     
    239239>>> Place.objects.get(pk=ident)
    240240<Place: Guido's All New House of Pasta the place>
    241241>>> xx = Restaurant.objects.create(name='a', address='xx', serves_hot_dogs=True, serves_pizza=False)
    242242
    243243# This should delete both Restuarants, plus the related places, plus the ItalianRestaurant.
    244244>>> Restaurant.objects.all().delete()
    245245
    246246>>> Place.objects.get(pk=ident)
    247247Traceback (most recent call last):
    248248...
    249 DoesNotExist: Place matching query does not exist.
     249DoesNotExist: Place matching query (`pk=1`) does not exist.
    250250
    251251>>> ItalianRestaurant.objects.get(pk=ident)
    252252Traceback (most recent call last):
    253253...
    254 DoesNotExist: ItalianRestaurant matching query does not exist.
     254DoesNotExist: ItalianRestaurant matching query (`pk=1`) does not exist.
    255255
    256256# Regression test for #6755
    257257>>> r = Restaurant(serves_pizza=False)
    258258>>> r.save()
    259259>>> r.id == r.place_ptr_id
    260260True
    261261>>> orig_id = r.id
    262262>>> r = Restaurant(place_ptr_id=orig_id, serves_pizza=True)
    263263>>> r.save()
    264264>>> r.id == orig_id
  • tests/regressiontests/reverse_single_related/models.py

     
    4242
    4343# If the manager is marked "use_for_related_fields", it'll get used instead
    4444# of the "bare" queryset. Usually you'd define this as a property on the class,
    4545# but this approximates that in a way that's easier in tests.
    4646
    4747>>> Source.objects.use_for_related_fields = True
    4848>>> private_item = Item.objects.get(pk=private_item.pk)
    4949>>> private_item.source
    5050Traceback (most recent call last):
    5151    ...
    52 DoesNotExist: Source matching query does not exist.
     52DoesNotExist: Source matching query (`id__exact=2`) does not exist.
    5353
    5454"""}
Back to Top