From 293fd5da5b8c7b79bd34ef793ab45c1bb8ac69ea Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Fri, 30 Jan 2015 11:23:20 -0500 Subject: [PATCH] Reverted "Fixed #6785 -- Made QuerySet.get() fetch a limited number of rows." This reverts commit da79ccca1d34f427952cce4555e598a700adb8de. This optimized the unsuccessful case at the expense of the successful one. --- django/db/models/query.py | 16 ++++------------ tests/basic/tests.py | 26 +------------------------- 2 files changed, 5 insertions(+), 37 deletions(-) diff --git a/django/db/models/query.py b/django/db/models/query.py index b2d210f2bc..016f89b198 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -23,10 +23,6 @@ from django.utils import six from django.utils import timezone from django.utils.version import get_version -# The maximum number (one less than the max to be precise) of results to fetch -# in a get() query -MAX_GET_RESULTS = 20 - # The maximum number of items to display in a QuerySet.__repr__ REPR_OUTPUT_SIZE = 20 @@ -379,21 +375,17 @@ class QuerySet(object): clone = self.filter(*args, **kwargs) if self.query.can_filter(): clone = clone.order_by() - if (not clone.query.select_for_update or - connections[self.db].features.supports_select_for_update_with_limit): - clone = clone[:MAX_GET_RESULTS + 1] num = len(clone) if num == 1: return clone._result_cache[0] if not num: raise self.model.DoesNotExist( "%s matching query does not exist." % - self.model._meta.object_name) - raise self.model.MultipleObjectsReturned( - "get() returned more than one %s -- it returned %s!" % ( - self.model._meta.object_name, - num if num <= MAX_GET_RESULTS else 'more than %s' % MAX_GET_RESULTS + self.model._meta.object_name ) + raise self.model.MultipleObjectsReturned( + "get() returned more than one %s -- it returned %s!" % + (self.model._meta.object_name, num) ) def create(self, **kwargs): diff --git a/tests/basic/tests.py b/tests/basic/tests.py index 3f91835f16..ad21b2b1c1 100644 --- a/tests/basic/tests.py +++ b/tests/basic/tests.py @@ -10,7 +10,7 @@ from django.db import DatabaseError from django.db.models.fields import Field from django.db.models.fields.related import ForeignObjectRel from django.db.models.manager import BaseManager -from django.db.models.query import QuerySet, EmptyQuerySet, MAX_GET_RESULTS +from django.db.models.query import QuerySet, EmptyQuerySet from django.test import TestCase, TransactionTestCase, skipIfDBFeature, skipUnlessDBFeature from django.utils import six from django.utils.translation import ugettext_lazy @@ -178,30 +178,6 @@ class ModelTest(TestCase): self.assertNotEqual(Article.objects.get(id__exact=a1.id), Article.objects.get(id__exact=a2.id)) - def test_multiple_objects_max_num_fetched(self): - """ - #6785 - get() should fetch a limited number of results. - """ - Article.objects.bulk_create( - Article(headline='Area %s' % i, pub_date=datetime(2005, 7, 28)) - for i in range(MAX_GET_RESULTS) - ) - six.assertRaisesRegex( - self, - MultipleObjectsReturned, - "get\(\) returned more than one Article -- it returned %d!" % MAX_GET_RESULTS, - Article.objects.get, - headline__startswith='Area', - ) - Article.objects.create(headline='Area %s' % MAX_GET_RESULTS, pub_date=datetime(2005, 7, 28)) - six.assertRaisesRegex( - self, - MultipleObjectsReturned, - "get\(\) returned more than one Article -- it returned more than %d!" % MAX_GET_RESULTS, - Article.objects.get, - headline__startswith='Area', - ) - @skipUnlessDBFeature('supports_microsecond_precision') def test_microsecond_precision(self): # In PostgreSQL, microsecond-level precision is available.