Author: jkocherhans
Date: 2010-02-24 08:42:03 -0600 (Wed, 24 Feb 2010)
New Revision: 12568

Modified:
   django/branches/releases/1.1.X/
   django/branches/releases/1.1.X/django/db/models/base.py
   django/branches/releases/1.1.X/tests/modeltests/model_inheritance/models.py
   django/branches/releases/1.1.X/tests/modeltests/proxy_models/models.py
Log:
[1.1.X] Fixed #12152. DoesNotExist and MultipleObjectsReturned now subclass 
their parent model's exceptions. Backport of r12567 from trunk.



Property changes on: django/branches/releases/1.1.X
___________________________________________________________________
Name: svnmerge-integrated
   - 
/django/trunk:1-11500,11523,11527-11528,11531-11552,11554,11577,11579-11581,11588-11589,11591-11592,11596-11599,11601-11617,11619-11626,11628-11635,11637-11638,11643-11644,11648-11653,11656,11670,11678,11681,11684,11686,11688,11691,11693,11695,11697,11699,11701,11703,11705,11707,11714,11719,11732,11734,11740,11748,11751,11753,11756,11760,11800,11802,11808,11815,11817,11820,11822,11824,11826,11828,11831,11833,11835,11837,11839,11841,11844,11857,11864,11874,11876,11878,11885,11898,11901,11905,11909,11912,11914,11917,11938,11953,11961,11977,11979,11984,11986,11988,11990,11992,11994,11996,11998,12001,12004,12006,12011,12022,12024,12044-12045,12048,12054-12056,12059,12064,12066,12068,12070,12079,12086,12088,12104,12118,12132,12137-12138,12140-12141,12144,12150-12152,12220-12221,12229,12249,12253,12276,12282,12284,12293,12313,12317-12324,12333,12341,12343,12346,12353,12362,12379,12384,12405,12492,12498,12523,12526,12533,12535,12537,12539,12541,12548,12556
   + 
/django/trunk:1-11500,11523,11527-11528,11531-11552,11554,11577,11579-11581,11588-11589,11591-11592,11596-11599,11601-11617,11619-11626,11628-11635,11637-11638,11643-11644,11648-11653,11656,11670,11678,11681,11684,11686,11688,11691,11693,11695,11697,11699,11701,11703,11705,11707,11714,11719,11732,11734,11740,11748,11751,11753,11756,11760,11800,11802,11808,11815,11817,11820,11822,11824,11826,11828,11831,11833,11835,11837,11839,11841,11844,11857,11864,11874,11876,11878,11885,11898,11901,11905,11909,11912,11914,11917,11938,11953,11961,11977,11979,11984,11986,11988,11990,11992,11994,11996,11998,12001,12004,12006,12011,12022,12024,12044-12045,12048,12054-12056,12059,12064,12066,12068,12070,12079,12086,12088,12104,12118,12132,12137-12138,12140-12141,12144,12150-12152,12220-12221,12229,12249,12253,12276,12282,12284,12293,12313,12317-12324,12333,12341,12343,12346,12353,12362,12379,12384,12405,12492,12498,12523,12526,12533,12535,12537,12539,12541,12548,12556,12567

Modified: django/branches/releases/1.1.X/django/db/models/base.py
===================================================================
--- django/branches/releases/1.1.X/django/db/models/base.py     2010-02-24 
14:32:11 UTC (rev 12567)
+++ django/branches/releases/1.1.X/django/db/models/base.py     2010-02-24 
14:42:03 UTC (rev 12568)
@@ -55,10 +55,14 @@
 
         new_class.add_to_class('_meta', Options(meta, **kwargs))
         if not abstract:
-            new_class.add_to_class('DoesNotExist',
-                    subclass_exception('DoesNotExist', ObjectDoesNotExist, 
module))
-            new_class.add_to_class('MultipleObjectsReturned',
-                    subclass_exception('MultipleObjectsReturned', 
MultipleObjectsReturned, module))
+            new_class.add_to_class('DoesNotExist', 
subclass_exception('DoesNotExist',
+                    tuple(x.DoesNotExist
+                            for x in parents if hasattr(x, '_meta') and not 
x._meta.abstract)
+                                    or (ObjectDoesNotExist,), module))
+            new_class.add_to_class('MultipleObjectsReturned', 
subclass_exception('MultipleObjectsReturned',
+                    tuple(x.MultipleObjectsReturned
+                            for x in parents if hasattr(x, '_meta') and not 
x._meta.abstract)
+                                    or (MultipleObjectsReturned,), module))
             if base_meta and not base_meta.abstract:
                 # Non-abstract child classes inherit some attributes from their
                 # non-abstract parent (unless an ABC comes before it in the
@@ -681,8 +685,8 @@
 
 if sys.version_info < (2, 5):
     # Prior to Python 2.5, Exception was an old-style class
-    def subclass_exception(name, parent, unused):
-        return types.ClassType(name, (parent,), {})
+    def subclass_exception(name, parents, unused):
+        return types.ClassType(name, parents, {})
 else:
-    def subclass_exception(name, parent, module):
-        return type(name, (parent,), {'__module__': module})
+    def subclass_exception(name, parents, module):
+        return type(name, parents, {'__module__': module})

Modified: 
django/branches/releases/1.1.X/tests/modeltests/model_inheritance/models.py
===================================================================
--- django/branches/releases/1.1.X/tests/modeltests/model_inheritance/models.py 
2010-02-24 14:32:11 UTC (rev 12567)
+++ django/branches/releases/1.1.X/tests/modeltests/model_inheritance/models.py 
2010-02-24 14:42:03 UTC (rev 12568)
@@ -38,6 +38,9 @@
     class Meta:
         pass
 
+class StudentWorker(Student, Worker):
+    pass
+
 #
 # Abstract base classes with related models
 #
@@ -151,6 +154,32 @@
     ...
 AttributeError: type object 'CommonInfo' has no attribute 'objects'
 
+# A StudentWorker which does not exist is both a Student and Worker which does 
not exist.
+>>> try:
+...     StudentWorker.objects.get(id=1)
+... except Student.DoesNotExist:
+...     pass
+>>> try:
+...     StudentWorker.objects.get(id=1)
+... except Worker.DoesNotExist:
+...     pass
+
+# MultipleObjectsReturned is also inherited.
+>>> sw1 = StudentWorker()
+>>> sw1.name = 'Wilma'
+>>> sw1.age = 35
+>>> sw1.save()
+>>> sw2 = StudentWorker()
+>>> sw2.name = 'Betty'
+>>> sw2.age = 34
+>>> sw2.save()
+>>> try:
+...     StudentWorker.objects.get(id__lt=10)
+... except Student.MultipleObjectsReturned:
+...     pass
+... except Worker.MultipleObjectsReturned:
+...     pass
+
 # Create a Post
 >>> post = Post(title='Lorem Ipsum')
 >>> post.save()
@@ -242,6 +271,18 @@
     ...
 DoesNotExist: ItalianRestaurant matching query does not exist.
 
+# An ItalianRestaurant which does not exist is also a Place which does not 
exist.
+>>> try:
+...     ItalianRestaurant.objects.get(name='The Noodle Void')
+... except Place.DoesNotExist:
+...     pass
+
+# MultipleObjectsReturned is also inherited.
+>>> try:
+...     Restaurant.objects.get(id__lt=10)
+... except Place.MultipleObjectsReturned:
+...     pass
+
 # Related objects work just as they normally do.
 
 >>> s1 = Supplier(name="Joe's Chickens", address='123 Sesame St')

Modified: django/branches/releases/1.1.X/tests/modeltests/proxy_models/models.py
===================================================================
--- django/branches/releases/1.1.X/tests/modeltests/proxy_models/models.py      
2010-02-24 14:32:11 UTC (rev 12567)
+++ django/branches/releases/1.1.X/tests/modeltests/proxy_models/models.py      
2010-02-24 14:42:03 UTC (rev 12568)
@@ -205,6 +205,26 @@
 >>> MyPersonProxy.objects.all()
 [<MyPersonProxy: Bazza del Frob>, <MyPersonProxy: Foo McBar>, <MyPersonProxy: 
homer>]
 
+# Proxy models are included in the ancestors for a model's DoesNotExist and 
MultipleObjectsReturned
+>>> try:
+...     MyPersonProxy.objects.get(name='Zathras')
+... except Person.DoesNotExist:
+...     pass
+>>> try:
+...     MyPersonProxy.objects.get(id__lt=10)
+... except Person.MultipleObjectsReturned:
+...     pass
+>>> try:
+...     StatusPerson.objects.get(name='Zathras')
+... except Person.DoesNotExist:
+...     pass
+>>> sp1 = StatusPerson.objects.create(name='Bazza Jr.')
+>>> sp2 = StatusPerson.objects.create(name='Foo Jr.')
+>>> try:
+...     StatusPerson.objects.get(id__lt=10)
+... except Person.MultipleObjectsReturned:
+...     pass
+
 # And now for some things that shouldn't work...
 #
 # All base classes must be non-abstract

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to