Ticket #12152: 12152-3.diff

File 12152-3.diff, 4.9 KB (added by mattmcc, 5 years ago)

Remove unnecessary uses of getattr

  • django/db/models/base.py

     
    5252
    5353        new_class.add_to_class('_meta', Options(meta, **kwargs))
    5454        if not abstract:
    55             new_class.add_to_class('DoesNotExist',
    56                     subclass_exception('DoesNotExist', ObjectDoesNotExist, module))
    57             new_class.add_to_class('MultipleObjectsReturned',
    58                     subclass_exception('MultipleObjectsReturned', MultipleObjectsReturned, module))
     55            new_class.add_to_class('DoesNotExist', subclass_exception('DoesNotExist',
     56                    tuple(x.DoesNotExist
     57                            for x in parents if hasattr(x, '_meta') and not x._meta.abstract)
     58                                    or ObjectDoesNotExist, module))
     59            new_class.add_to_class('MultipleObjectsReturned', subclass_exception('MultipleObjectsReturned',
     60                    tuple(x.MultipleObjectsReturned
     61                            for x in parents if hasattr(x, '_meta') and not x._meta.abstract)
     62                                    or MultipleObjectsReturned, module))
    5963            if base_meta and not base_meta.abstract:
    6064                # Non-abstract child classes inherit some attributes from their
    6165                # non-abstract parent (unless an ABC comes before it in the
     
    920924if sys.version_info < (2, 5):
    921925    # Prior to Python 2.5, Exception was an old-style class
    922926    def subclass_exception(name, parent, unused):
    923         return types.ClassType(name, (parent,), {})
     927        return types.ClassType(name,
     928                    (parent,) if type(parent) == type else parent, {})
    924929else:
    925930    def subclass_exception(name, parent, module):
    926         return type(name, (parent,), {'__module__': module})
     931        return type(name, (parent,) if type(parent) == type else parent,
     932                    {'__module__': module})
  • tests/modeltests/proxy_models/models.py

     
    206206>>> MyPersonProxy.objects.all()
    207207[<MyPersonProxy: Bazza del Frob>, <MyPersonProxy: Foo McBar>, <MyPersonProxy: homer>]
    208208
     209# Proxy models are included in the ancestors for a model's DoesNotExist and MultipleObjectsReturned
     210>>> try:
     211...     MyPersonProxy.objects.get(name='Zathras')
     212... except Person.DoesNotExist:
     213...     pass
     214>>> try:
     215...     MyPersonProxy.objects.get(id__lt=10)
     216... except Person.MultipleObjectsReturned:
     217...     pass
     218>>> try:
     219...     StatusPerson.objects.get(name='Zathras')
     220... except Person.DoesNotExist:
     221...     pass
     222>>> sp1 = StatusPerson.objects.create(name='Bazza Jr.')
     223>>> sp2 = StatusPerson.objects.create(name='Foo Jr.')
     224>>> try:
     225...     StatusPerson.objects.get(id__lt=10)
     226... except Person.MultipleObjectsReturned:
     227...     pass
     228
    209229# And now for some things that shouldn't work...
    210230#
    211231# All base classes must be non-abstract
  • tests/modeltests/model_inheritance/models.py

     
    3838    class Meta:
    3939        pass
    4040
     41class StudentWorker(Student, Worker):
     42    pass
     43
    4144#
    4245# Abstract base classes with related models
    4346#
     
    176179    ...
    177180AttributeError: type object 'CommonInfo' has no attribute 'objects'
    178181
     182# A StudentWorker which does not exist is both a Student and Worker which does not exist.
     183>>> try:
     184...     StudentWorker.objects.get(id=1)
     185... except Student.DoesNotExist:
     186...     pass
     187>>> try:
     188...     StudentWorker.objects.get(id=1)
     189... except Worker.DoesNotExist:
     190...     pass
     191
     192# MultipleObjectsReturned is also inherited.
     193>>> sw1 = StudentWorker()
     194>>> sw1.name = 'Wilma'
     195>>> sw1.age = 35
     196>>> sw1.save()
     197>>> sw2 = StudentWorker()
     198>>> sw2.name = 'Betty'
     199>>> sw2.age = 34
     200>>> sw2.save()
     201>>> try:
     202...     StudentWorker.objects.get(id__lt=10)
     203... except Student.MultipleObjectsReturned:
     204...     pass
     205... except Worker.MultipleObjectsReturned:
     206...     pass
     207
    179208# Create a Post
    180209>>> post = Post(title='Lorem Ipsum')
    181210>>> post.save()
     
    267296    ...
    268297DoesNotExist: ItalianRestaurant matching query does not exist.
    269298
     299# An ItalianRestaurant which does not exist is also a Place which does not exist.
     300>>> try:
     301...     ItalianRestaurant.objects.get(name='The Noodle Void')
     302... except Place.DoesNotExist:
     303...     pass
     304
     305# MultipleObjectsReturned is also inherited.
     306>>> try:
     307...     Restaurant.objects.get(id__lt=10)
     308... except Place.MultipleObjectsReturned:
     309...     pass
     310
    270311# Related objects work just as they normally do.
    271312
    272313>>> s1 = Supplier(name="Joe's Chickens", address='123 Sesame St')
Back to Top