diff --git a/django/db/models/query.py b/django/db/models/query.py
index 324554e..778154d 100644
a
|
b
|
class QuerySet(object):
|
340 | 340 | """ |
341 | 341 | clone = self.filter(*args, **kwargs) |
342 | 342 | if self.query.can_filter(): |
343 | | clone = clone.order_by() |
| 343 | clone = clone.order_by()[:2] |
344 | 344 | num = len(clone) |
345 | 345 | if num == 1: |
346 | 346 | return clone._result_cache[0] |
347 | 347 | if not num: |
348 | 348 | raise self.model.DoesNotExist("%s matching query does not exist." |
349 | 349 | % self.model._meta.object_name) |
350 | | raise self.model.MultipleObjectsReturned("get() returned more than one %s -- it returned %s! Lookup parameters were %s" |
351 | | % (self.model._meta.object_name, num, kwargs)) |
| 350 | raise self.model.MultipleObjectsReturned("get() query returned more than one %s. Lookup parameters were %s" |
| 351 | % (self.model._meta.object_name, kwargs)) |
352 | 352 | |
353 | 353 | def create(self, **kwargs): |
354 | 354 | """ |
diff --git a/tests/modeltests/basic/tests.py b/tests/modeltests/basic/tests.py
index 966798d..f0a2f16 100644
a
|
b
|
|
1 | 1 | from datetime import datetime |
2 | 2 | |
3 | | from django.core.exceptions import ObjectDoesNotExist |
| 3 | from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned |
4 | 4 | from django.db import models, DEFAULT_DB_ALIAS, connection |
5 | 5 | from django.db.models.fields import FieldDoesNotExist |
6 | 6 | from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature |
… |
… |
class ModelTest(TestCase):
|
115 | 115 | b = Article.objects.get(pk=a.id) |
116 | 116 | self.assertEqual(a, b) |
117 | 117 | |
| 118 | # Create a very similar object |
| 119 | a = Article( |
| 120 | id=None, |
| 121 | headline='Area man programs in Python', |
| 122 | pub_date=datetime(2005, 7, 28), |
| 123 | ) |
| 124 | a.save() |
| 125 | |
| 126 | self.assertEqual(Article.objects.count(), 2) |
| 127 | |
| 128 | # Django raises an Article.MultipleObjectsReturned exception if the |
| 129 | # lookup matches more than one object |
| 130 | self.assertRaisesRegexp( |
| 131 | MultipleObjectsReturned, |
| 132 | "get\(\) query returned more than one Article\. Lookup parameters were {'headline__startswith': 'Area'}", |
| 133 | Article.objects.get, |
| 134 | headline__startswith='Area', |
| 135 | ) |
| 136 | |
| 137 | self.assertRaisesRegexp( |
| 138 | MultipleObjectsReturned, |
| 139 | "get\(\) query returned more than one Article\. Lookup parameters were {'pub_date__year': 2005}", |
| 140 | Article.objects.get, |
| 141 | pub_date__year=2005, |
| 142 | ) |
| 143 | |
| 144 | self.assertRaisesRegexp( |
| 145 | MultipleObjectsReturned, |
| 146 | "get\(\) query returned more than one Article\. Lookup parameters were {'pub_date__month': 7, 'pub_date__year': 2005}", |
| 147 | Article.objects.get, |
| 148 | pub_date__year=2005, |
| 149 | pub_date__month=7, |
| 150 | ) |
| 151 | |
118 | 152 | def test_object_creation(self): |
119 | 153 | # Create an Article. |
120 | 154 | a = Article( |