=== modified file 'django/core/exceptions.py'
|
|
|
4 | 4 | "The requested object does not exist" |
5 | 5 | silent_variable_failure = True |
6 | 6 | |
| 7 | class MultipleObjectsReturned(Exception): |
| 8 | "The query returned multiple objects when only one was expected." |
| 9 | pass |
| 10 | |
7 | 11 | class SuspiciousOperation(Exception): |
8 | 12 | "The user did something suspicious" |
9 | 13 | pass |
=== modified file 'django/db/models/base.py'
|
|
|
1 | 1 | import django.db.models.manipulators |
2 | 2 | import django.db.models.manager |
3 | 3 | from django.core import validators |
4 | | from django.core.exceptions import ObjectDoesNotExist |
| 4 | from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned |
5 | 5 | from django.db.models.fields import AutoField, ImageField, FieldDoesNotExist |
6 | 6 | from django.db.models.fields.related import OneToOneRel, ManyToOneRel |
7 | 7 | from django.db.models.query import delete_objects |
… |
… |
|
28 | 28 | new_class = type.__new__(cls, name, bases, {'__module__': attrs.pop('__module__')}) |
29 | 29 | new_class.add_to_class('_meta', Options(attrs.pop('Meta', None))) |
30 | 30 | new_class.add_to_class('DoesNotExist', types.ClassType('DoesNotExist', (ObjectDoesNotExist,), {})) |
| 31 | new_class.add_to_class('MultipleObjectsReturned', |
| 32 | types.ClassType('MultipleObjectsReturned', (MultipleObjectsReturned,), {})) |
31 | 33 | |
32 | 34 | # Build complete list of parents |
33 | 35 | for base in bases: |
=== modified file 'django/db/models/query.py'
|
|
|
236 | 236 | obj_list = list(clone) |
237 | 237 | if len(obj_list) < 1: |
238 | 238 | raise self.model.DoesNotExist, "%s matching query does not exist." % self.model._meta.object_name |
239 | | 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) |
| 239 | elif len(obj_list) > 1: |
| 240 | 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) |
240 | 241 | return obj_list[0] |
241 | 242 | |
242 | 243 | def create(self, **kwargs): |