From 7a62bac56ea484adb21f49ee88a322d1e08f74ba Mon Sep 17 00:00:00 2001 From: Luke Plant Date: Sat, 6 May 2006 18:46:53 +0000 Subject: [PATCH] Made QuerySet slicing return IndexError instead of DoesNotExist (and related changes). git-svn-id: http://code.djangoproject.com/svn/django/trunk@2859 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/base.py | 5 ++++- django/db/models/query.py | 10 +++++----- django/views/defaults.py | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/django/db/models/base.py b/django/db/models/base.py index 932b533291..279e95fd19 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -264,7 +264,10 @@ class Model(object): q = self.__class__._default_manager.order_by((not is_next and '-' or '') + field.name, (not is_next and '-' or '') + self._meta.pk.name) q._where.append(where) q._params.extend([param, param, getattr(self, self._meta.pk.attname)]) - return q[0] + try: + return q[0] + except IndexError, e: + raise self.DoesNotExist, e.args def _get_next_or_previous_in_order(self, is_next): cachename = "__%s_order_cache" % is_next diff --git a/django/db/models/query.py b/django/db/models/query.py index 096e414a39..ea7533a67d 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -128,12 +128,12 @@ class QuerySet(object): else: return list(self._clone(_offset=offset, _limit=limit))[::k.step] else: - return self._clone(_offset=k, _limit=1).get() + try: + return self._clone(_offset=k, _limit=1).get() + except self.model.DoesNotExist, e: + raise IndexError, e.args else: - try: - return self._result_cache[k] - except IndexError: - raise self.model.DoesNotExist, "%s matching query does not exist." % self.model._meta.object_name + return self._result_cache[k] def __and__(self, other): combined = self._combine(other) diff --git a/django/views/defaults.py b/django/views/defaults.py index d5460a7495..2191537f07 100644 --- a/django/views/defaults.py +++ b/django/views/defaults.py @@ -35,7 +35,7 @@ def shortcut(request, content_type_id, object_id): if field.rel.to is Site: try: object_domain = getattr(obj, field.name).all()[0].domain - except Site.DoesNotExist: + except IndexError: pass if object_domain is not None: break