From da79ccca1d34f427952cce4555e598a700adb8de Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Tue, 2 Jul 2013 11:25:01 -0400 Subject: [PATCH] Fixed #6785 -- Made QuerySet.get() fetch a limited number of rows. Thanks Patryk Zawadzki. --- django/db/models/query.py | 12 ++++++++++-- tests/basic/tests.py | 24 +++++++++++++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/django/db/models/query.py b/django/db/models/query.py index 8baef82bc7..086cc6dd71 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -19,6 +19,10 @@ from django.utils.functional import partition from django.utils import six from django.utils import timezone +# 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 @@ -297,6 +301,7 @@ class QuerySet(object): clone = self.filter(*args, **kwargs) if self.query.can_filter(): clone = clone.order_by() + clone = clone[:MAX_GET_RESULTS + 1] num = len(clone) if num == 1: return clone._result_cache[0] @@ -305,8 +310,11 @@ class QuerySet(object): "%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)) + "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 + ) + ) def create(self, **kwargs): """ diff --git a/tests/basic/tests.py b/tests/basic/tests.py index 4b79c47be5..fb21b11279 100644 --- a/tests/basic/tests.py +++ b/tests/basic/tests.py @@ -6,7 +6,7 @@ import threading from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned from django.db import connections, DEFAULT_DB_ALIAS from django.db.models.fields import Field, FieldDoesNotExist -from django.db.models.query import QuerySet, EmptyQuerySet, ValuesListQuerySet +from django.db.models.query import QuerySet, EmptyQuerySet, ValuesListQuerySet, MAX_GET_RESULTS from django.test import TestCase, TransactionTestCase, skipIfDBFeature, skipUnlessDBFeature from django.utils import six from django.utils.translation import ugettext_lazy @@ -155,6 +155,28 @@ class ModelTest(TestCase): pub_date__month=7, ) + 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', + ) + def test_object_creation(self): # Create an Article. a = Article(