Ticket #3511: 3511.2.diff

File 3511.2.diff, 4.2 KB (added by cheeming, 17 years ago)

Base of trunk r6781, fixed test and updated documentation

  • 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
     
    3535        new_class = type.__new__(cls, name, bases, {'__module__': attrs.pop('__module__')})
    3636        new_class.add_to_class('_meta', Options(attrs.pop('Meta', None)))
    3737        new_class.add_to_class('DoesNotExist', types.ClassType('DoesNotExist', (ObjectDoesNotExist,), {}))
     38        new_class.add_to_class('MultipleObjectsReturned',
     39            types.ClassType('MultipleObjectsReturned', (MultipleObjectsReturned, ), {}))
    3840
    3941        # Build complete list of parents
    4042        for base in bases:
  • django/db/models/query.py

     
    261261        obj_list = list(clone)
    262262        if len(obj_list) < 1:
    263263            raise self.model.DoesNotExist, "%s matching query does not exist." % self.model._meta.object_name
    264         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)
     264        elif len(obj_list) > 1:
     265            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)
    265266        return obj_list[0]
    266267
    267268    def create(self, **kwargs):
  • 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/shortcuts/__init__.py

     
    3838    klass may be a Model, Manager, or QuerySet object. All other passed
    3939    arguments and keyword arguments are used in the get() query.
    4040
    41     Note: Like with get(), an AssertionError will be raised if more than one
     41    Note: Like with get(), an MultipleObjectsReturned will be raised if more than one
    4242    object is found.
    4343    """
    4444    queryset = _get_queryset(klass)
  • tests/modeltests/get_object_or_404/models.py

     
    7878>>> get_object_or_404(Author.objects.all())
    7979Traceback (most recent call last):
    8080...
    81 AssertionError: get() returned more than one Author -- it returned ...! Lookup parameters were {}
     81MultipleObjectsReturned: get() returned more than one Author -- it returned ...! Lookup parameters were {}
    8282
    8383# Using an EmptyQuerySet raises a Http404 error.
    8484>>> get_object_or_404(Article.objects.none(), title__contains="Run")
  • docs/shortcuts.txt

     
    9898        except MyModel.DoesNotExist:
    9999            raise Http404
    100100
    101 Note: As with ``get()``, an ``AssertionError`` will be raised if more than
    102 one object is found.
     101Note: As with ``get()``, an ``MultipleObjectsReturned`` exception will be
     102raised if more than one object is found.
    103103
    104104.. _get(): ../db-api/#get-kwargs
    105105
Back to Top