Code

Ticket #3511: 3511.diff

File 3511.diff, 2.4 KB (added by Gary Wilson <gary.wilson@…>, 7 years ago)
Line 
1=== modified file 'django/core/exceptions.py'
2--- django/core/exceptions.py   2007-02-13 16:11:28 +0000
3+++ django/core/exceptions.py   2007-02-16 19:36:15 +0000
4@@ -4,6 +4,10 @@
5     "The requested object does not exist"
6     silent_variable_failure = True
7 
8+class MultipleObjectsReturned(Exception):
9+    "The query returned multiple objects when only one was expected."
10+    pass
11+
12 class SuspiciousOperation(Exception):
13     "The user did something suspicious"
14     pass
15
16=== modified file 'django/db/models/base.py'
17--- django/db/models/base.py    2007-02-13 16:11:28 +0000
18+++ django/db/models/base.py    2007-02-16 19:38:58 +0000
19@@ -1,7 +1,7 @@
20 import django.db.models.manipulators
21 import django.db.models.manager
22 from django.core import validators
23-from django.core.exceptions import ObjectDoesNotExist
24+from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
25 from django.db.models.fields import AutoField, ImageField, FieldDoesNotExist
26 from django.db.models.fields.related import OneToOneRel, ManyToOneRel
27 from django.db.models.query import delete_objects
28@@ -28,6 +28,8 @@
29         new_class = type.__new__(cls, name, bases, {'__module__': attrs.pop('__module__')})
30         new_class.add_to_class('_meta', Options(attrs.pop('Meta', None)))
31         new_class.add_to_class('DoesNotExist', types.ClassType('DoesNotExist', (ObjectDoesNotExist,), {}))
32+        new_class.add_to_class('MultipleObjectsReturned',
33+            types.ClassType('MultipleObjectsReturned', (MultipleObjectsReturned,), {}))
34 
35         # Build complete list of parents
36         for base in bases:
37
38=== modified file 'django/db/models/query.py'
39--- django/db/models/query.py   2007-02-16 19:21:54 +0000
40+++ django/db/models/query.py   2007-02-16 19:42:53 +0000
41@@ -236,7 +236,8 @@
42         obj_list = list(clone)
43         if len(obj_list) < 1:
44             raise self.model.DoesNotExist, "%s matching query does not exist." % self.model._meta.object_name
45-        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)
46+        elif len(obj_list) > 1:
47+            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)
48         return obj_list[0]
49 
50     def create(self, **kwargs):
51