Ticket #3511: 3511.diff

File 3511.diff, 2.4 KB (added by Gary Wilson <gary.wilson@…>, 18 years ago)
  • django/core/exceptions.py

    === modified file 'django/core/exceptions.py'
     
    44    "The requested object does not exist"
    55    silent_variable_failure = True
    66
     7class MultipleObjectsReturned(Exception):
     8    "The query returned multiple objects when only one was expected."
     9    pass
     10
    711class SuspiciousOperation(Exception):
    812    "The user did something suspicious"
    913    pass
  • django/db/models/base.py

    === modified file 'django/db/models/base.py'
     
    11import django.db.models.manipulators
    22import django.db.models.manager
    33from django.core import validators
    4 from django.core.exceptions import ObjectDoesNotExist
     4from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
    55from django.db.models.fields import AutoField, ImageField, FieldDoesNotExist
    66from django.db.models.fields.related import OneToOneRel, ManyToOneRel
    77from django.db.models.query import delete_objects
     
    2828        new_class = type.__new__(cls, name, bases, {'__module__': attrs.pop('__module__')})
    2929        new_class.add_to_class('_meta', Options(attrs.pop('Meta', None)))
    3030        new_class.add_to_class('DoesNotExist', types.ClassType('DoesNotExist', (ObjectDoesNotExist,), {}))
     31        new_class.add_to_class('MultipleObjectsReturned',
     32            types.ClassType('MultipleObjectsReturned', (MultipleObjectsReturned,), {}))
    3133
    3234        # Build complete list of parents
    3335        for base in bases:
  • django/db/models/query.py

    === modified file 'django/db/models/query.py'
     
    236236        obj_list = list(clone)
    237237        if len(obj_list) < 1:
    238238            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)
    240241        return obj_list[0]
    241242
    242243    def create(self, **kwargs):
Back to Top