Ticket #15361: 15361.diff

File 15361.diff, 3.0 KB (added by mmcnickle, 14 years ago)

Patch with test

  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index 324554e..778154d 100644
    a b class QuerySet(object):  
    340340        """
    341341        clone = self.filter(*args, **kwargs)
    342342        if self.query.can_filter():
    343             clone = clone.order_by()
     343            clone = clone.order_by()[:2]
    344344        num = len(clone)
    345345        if num == 1:
    346346            return clone._result_cache[0]
    347347        if not num:
    348348            raise self.model.DoesNotExist("%s matching query does not exist."
    349349                    % 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))
    352352
    353353    def create(self, **kwargs):
    354354        """
  • tests/modeltests/basic/tests.py

    diff --git a/tests/modeltests/basic/tests.py b/tests/modeltests/basic/tests.py
    index 966798d..f0a2f16 100644
    a b  
    11from datetime import datetime
    22
    3 from django.core.exceptions import ObjectDoesNotExist
     3from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
    44from django.db import models, DEFAULT_DB_ALIAS, connection
    55from django.db.models.fields import FieldDoesNotExist
    66from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
    class ModelTest(TestCase):  
    115115        b = Article.objects.get(pk=a.id)
    116116        self.assertEqual(a, b)
    117117
     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
    118152    def test_object_creation(self):
    119153        # Create an Article.
    120154        a = Article(
Back to Top