Django

Code

Changeset 6838

Show
Ignore:
Timestamp:
12/02/07 12:21:07 (7 months ago)
Author:
mtredinnick
Message:

Fixed #3511 -- Changed QuerySet?.get() to return a MultipleObjectsReturned? exception, rather than an assertion error. Thanks, Gary Wilson and cheeming.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/exceptions.py

    r2809 r6838  
    44    "The requested object does not exist" 
    55    silent_variable_failure = True 
     6 
     7class MultipleObjectsReturned(Exception): 
     8    "The query returned multiple objects when only one was expected." 
     9    pass 
    610 
    711class SuspiciousOperation(Exception): 
  • django/trunk/django/db/models/base.py

    r6732 r6838  
    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 
     
    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 
  • django/trunk/django/db/models/query.py

    r6411 r6838  
    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 
  • django/trunk/django/shortcuts/__init__.py

    r6257 r6838  
    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    """ 
  • django/trunk/docs/shortcuts.txt

    r6234 r6838  
    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 
  • django/trunk/tests/modeltests/get_object_or_404/models.py

    r5876 r6838  
    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.