Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #3511 -- Changed QuerySet.get() to return a MultipleObjectsRetu…

…rned exception, rather than an assertion error. Thanks, Gary Wilson and cheeming.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6838 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 805c364fcf52f69498e15ddd8960baa3e4bc6d34 1 parent 5e5768a
Malcolm Tredinnick malcolmt authored
4 django/core/exceptions.py
View
@@ -4,6 +4,10 @@ class ObjectDoesNotExist(Exception):
"The requested object does not exist"
silent_variable_failure = True
+class MultipleObjectsReturned(Exception):
+ "The query returned multiple objects when only one was expected."
+ pass
+
class SuspiciousOperation(Exception):
"The user did something suspicious"
pass
4 django/db/models/base.py
View
@@ -1,7 +1,7 @@
import django.db.models.manipulators
import django.db.models.manager
from django.core import validators
-from django.core.exceptions import ObjectDoesNotExist
+from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
from django.db.models.fields import AutoField, ImageField, FieldDoesNotExist
from django.db.models.fields.related import OneToOneRel, ManyToOneRel
from django.db.models.query import delete_objects
@@ -35,6 +35,8 @@ def __new__(cls, name, bases, attrs):
new_class = type.__new__(cls, name, bases, {'__module__': attrs.pop('__module__')})
new_class.add_to_class('_meta', Options(attrs.pop('Meta', None)))
new_class.add_to_class('DoesNotExist', types.ClassType('DoesNotExist', (ObjectDoesNotExist,), {}))
+ new_class.add_to_class('MultipleObjectsReturned',
+ types.ClassType('MultipleObjectsReturned', (MultipleObjectsReturned, ), {}))
# Build complete list of parents
for base in bases:
3  django/db/models/query.py
View
@@ -261,7 +261,8 @@ def get(self, *args, **kwargs):
obj_list = list(clone)
if len(obj_list) < 1:
raise self.model.DoesNotExist, "%s matching query does not exist." % self.model._meta.object_name
- assert len(obj_list) == 1, "get() returned more than one %s -- it returned %s! Lookup parameters were %s" % (self.model._meta.object_name, len(obj_list), kwargs)
+ elif len(obj_list) > 1:
+ raise self.model.MultipleObjectsReturned, "get() returned more than one %s -- it returned %s! Lookup parameters were %s" % (self.model._meta.object_name, len(obj_list), kwargs)
return obj_list[0]
def create(self, **kwargs):
2  django/shortcuts/__init__.py
View
@@ -38,7 +38,7 @@ def get_object_or_404(klass, *args, **kwargs):
klass may be a Model, Manager, or QuerySet object. All other passed
arguments and keyword arguments are used in the get() query.
- Note: Like with get(), an AssertionError will be raised if more than one
+ Note: Like with get(), an MultipleObjectsReturned will be raised if more than one
object is found.
"""
queryset = _get_queryset(klass)
4 docs/shortcuts.txt
View
@@ -98,8 +98,8 @@ This example is equivalent to::
except MyModel.DoesNotExist:
raise Http404
-Note: As with ``get()``, an ``AssertionError`` will be raised if more than
-one object is found.
+Note: As with ``get()``, an ``MultipleObjectsReturned`` exception will be
+raised if more than one object is found.
.. _get(): ../db-api/#get-kwargs
2  tests/modeltests/get_object_or_404/models.py
View
@@ -78,7 +78,7 @@ def __unicode__(self):
>>> get_object_or_404(Author.objects.all())
Traceback (most recent call last):
...
-AssertionError: get() returned more than one Author -- it returned ...! Lookup parameters were {}
+MultipleObjectsReturned: get() returned more than one Author -- it returned ...! Lookup parameters were {}
# Using an EmptyQuerySet raises a Http404 error.
>>> get_object_or_404(Article.objects.none(), title__contains="Run")
Please sign in to comment.
Something went wrong with that request. Please try again.