Ticket #12152: 12152-2.diff
File 12152-2.diff, 4.9 KB (added by , 15 years ago) |
---|
-
django/db/models/base.py
49 49 50 50 new_class.add_to_class('_meta', Options(meta, **kwargs)) 51 51 if not abstract: 52 new_class.add_to_class('DoesNotExist', 53 subclass_exception('DoesNotExist', ObjectDoesNotExist, module)) 54 new_class.add_to_class('MultipleObjectsReturned', 55 subclass_exception('MultipleObjectsReturned', MultipleObjectsReturned, module)) 52 new_class.add_to_class('DoesNotExist', subclass_exception('DoesNotExist', 53 tuple(getattr(x, 'DoesNotExist') 54 for x in parents if hasattr(x, '_meta') and not x._meta.abstract) 55 or ObjectDoesNotExist, module)) 56 new_class.add_to_class('MultipleObjectsReturned', subclass_exception('MultipleObjectsReturned', 57 tuple(getattr(x, 'MultipleObjectsReturned') 58 for x in parents if hasattr(x, '_meta') and not x._meta.abstract) 59 or MultipleObjectsReturned, module)) 56 60 if base_meta and not base_meta.abstract: 57 61 # Non-abstract child classes inherit some attributes from their 58 62 # non-abstract parent (unless an ABC comes before it in the … … 680 684 if sys.version_info < (2, 5): 681 685 # Prior to Python 2.5, Exception was an old-style class 682 686 def subclass_exception(name, parent, unused): 683 return types.ClassType(name, (parent,), {}) 687 return types.ClassType(name, 688 (parent,) if type(parent) == type else parent, {}) 684 689 else: 685 690 def subclass_exception(name, parent, module): 686 return type(name, (parent,), {'__module__': module}) 691 return type(name, (parent,) if type(parent) == type else parent, 692 {'__module__': module}) -
tests/modeltests/proxy_models/models.py
205 205 >>> MyPersonProxy.objects.all() 206 206 [<MyPersonProxy: Bazza del Frob>, <MyPersonProxy: Foo McBar>, <MyPersonProxy: homer>] 207 207 208 # Proxy models are included in the ancestors for a model's DoesNotExist and MultipleObjectsReturned 209 >>> try: 210 ... MyPersonProxy.objects.get(name='Zathras') 211 ... except Person.DoesNotExist: 212 ... pass 213 >>> try: 214 ... MyPersonProxy.objects.get(id__lt=10) 215 ... except Person.MultipleObjectsReturned: 216 ... pass 217 >>> try: 218 ... StatusPerson.objects.get(name='Zathras') 219 ... except Person.DoesNotExist: 220 ... pass 221 >>> sp1 = StatusPerson.objects.create(name='Bazza Jr.') 222 >>> sp2 = StatusPerson.objects.create(name='Foo Jr.') 223 >>> try: 224 ... StatusPerson.objects.get(id__lt=10) 225 ... except Person.MultipleObjectsReturned: 226 ... pass 227 208 228 # And now for some things that shouldn't work... 209 229 # 210 230 # All base classes must be non-abstract -
tests/modeltests/model_inheritance/models.py
38 38 class Meta: 39 39 pass 40 40 41 class StudentWorker(Student, Worker): 42 pass 43 41 44 # 42 45 # Abstract base classes with related models 43 46 # … … 151 154 ... 152 155 AttributeError: type object 'CommonInfo' has no attribute 'objects' 153 156 157 # A StudentWorker which does not exist is both a Student and Worker which does not exist. 158 >>> try: 159 ... StudentWorker.objects.get(id=1) 160 ... except Student.DoesNotExist: 161 ... pass 162 >>> try: 163 ... StudentWorker.objects.get(id=1) 164 ... except Worker.DoesNotExist: 165 ... pass 166 167 # MultipleObjectsReturned is also inherited. 168 >>> sw1 = StudentWorker() 169 >>> sw1.name = 'Wilma' 170 >>> sw1.age = 35 171 >>> sw1.save() 172 >>> sw2 = StudentWorker() 173 >>> sw2.name = 'Betty' 174 >>> sw2.age = 34 175 >>> sw2.save() 176 >>> try: 177 ... StudentWorker.objects.get(id__lt=10) 178 ... except Student.MultipleObjectsReturned: 179 ... pass 180 ... except Worker.MultipleObjectsReturned: 181 ... pass 182 154 183 # Create a Post 155 184 >>> post = Post(title='Lorem Ipsum') 156 185 >>> post.save() … … 242 271 ... 243 272 DoesNotExist: ItalianRestaurant matching query does not exist. 244 273 274 # An ItalianRestaurant which does not exist is also a Place which does not exist. 275 >>> try: 276 ... ItalianRestaurant.objects.get(name='The Noodle Void') 277 ... except Place.DoesNotExist: 278 ... pass 279 280 # MultipleObjectsReturned is also inherited. 281 >>> try: 282 ... Restaurant.objects.get(id__lt=10) 283 ... except Place.MultipleObjectsReturned: 284 ... pass 285 245 286 # Related objects work just as they normally do. 246 287 247 288 >>> s1 = Supplier(name="Joe's Chickens", address='123 Sesame St')