[1.11.x] Fixed #28269 -- Fixed Model.__init__() crash on models with a field that has an instance only descriptor.
Regression in d2a26c1a90e837777dabdf3d67ceec4d2a70fb86. Backport of ed244199c72f5bbf33ab4547e06e69873d7271d0 from master
This commit is contained in:
parent
fa8346b9a9
commit
b7d6077517
@ -882,7 +882,13 @@ class Options(object):
|
|||||||
@cached_property
|
@cached_property
|
||||||
def _property_names(self):
|
def _property_names(self):
|
||||||
"""Return a set of the names of the properties defined on the model."""
|
"""Return a set of the names of the properties defined on the model."""
|
||||||
return frozenset({
|
names = []
|
||||||
attr for attr in
|
for name in dir(self.model):
|
||||||
dir(self.model) if isinstance(getattr(self.model, attr), property)
|
try:
|
||||||
})
|
attr = getattr(self.model, name)
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
if isinstance(attr, property):
|
||||||
|
names.append(name)
|
||||||
|
return frozenset(names)
|
||||||
|
@ -12,3 +12,6 @@ Bugfixes
|
|||||||
* Removed an incorrect deprecation warning about a missing ``renderer``
|
* Removed an incorrect deprecation warning about a missing ``renderer``
|
||||||
argument if a ``Widget.render()`` method accepts ``**kwargs``
|
argument if a ``Widget.render()`` method accepts ``**kwargs``
|
||||||
(:ticket:`28265`).
|
(:ticket:`28265`).
|
||||||
|
|
||||||
|
* Fixed a regression causing ``Model.__init__()`` to crash if a field has an
|
||||||
|
instance only descriptor (:ticket:`28269`).
|
||||||
|
@ -9,6 +9,13 @@ class Relation(models.Model):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class InstanceOnlyDescriptor(object):
|
||||||
|
def __get__(self, instance, cls=None):
|
||||||
|
if instance is None:
|
||||||
|
raise AttributeError('Instance only')
|
||||||
|
return 1
|
||||||
|
|
||||||
|
|
||||||
class AbstractPerson(models.Model):
|
class AbstractPerson(models.Model):
|
||||||
# DATA fields
|
# DATA fields
|
||||||
data_abstract = models.CharField(max_length=10)
|
data_abstract = models.CharField(max_length=10)
|
||||||
@ -43,6 +50,8 @@ class AbstractPerson(models.Model):
|
|||||||
def test_property(self):
|
def test_property(self):
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
test_instance_only_descriptor = InstanceOnlyDescriptor()
|
||||||
|
|
||||||
|
|
||||||
class BasePerson(AbstractPerson):
|
class BasePerson(AbstractPerson):
|
||||||
# DATA fields
|
# DATA fields
|
||||||
|
@ -276,4 +276,6 @@ class ParentListTests(SimpleTestCase):
|
|||||||
|
|
||||||
class PropertyNamesTests(SimpleTestCase):
|
class PropertyNamesTests(SimpleTestCase):
|
||||||
def test_person(self):
|
def test_person(self):
|
||||||
|
# Instance only descriptors don't appear in _property_names.
|
||||||
|
self.assertEqual(AbstractPerson().test_instance_only_descriptor, 1)
|
||||||
self.assertEqual(AbstractPerson._meta._property_names, frozenset(['pk', 'test_property']))
|
self.assertEqual(AbstractPerson._meta._property_names, frozenset(['pk', 'test_property']))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user