Fixed #12568 -- no error when accessing custom field's descriptor

The SubfieldBase's descriptor caused an AttributeError when accessed
from the class. Introspection didn't like that.

Patch by Trac alias supervacuo.
This commit is contained in:
Anssi Kääriäinen 2013-09-18 09:56:05 +03:00
parent 5be56d0e0d
commit 50633e7353
2 changed files with 16 additions and 2 deletions

View File

@ -30,7 +30,7 @@ class Creator(object):
def __get__(self, obj, type=None): def __get__(self, obj, type=None):
if obj is None: if obj is None:
raise AttributeError('Can only be accessed via an instance.') return self
return obj.__dict__[self.field.name] return obj.__dict__[self.field.name]
def __set__(self, obj, value): def __set__(self, obj, value):

View File

@ -1,5 +1,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import inspect
from django.core import serializers from django.core import serializers
from django.test import TestCase from django.test import TestCase
@ -74,7 +76,7 @@ class CustomField(TestCase):
m.delete() m.delete()
m1 = MyModel.objects.create(name="1", data=Small(1, 2)) m1 = MyModel.objects.create(name="1", data=Small(1, 2))
m2 = MyModel.objects.create(name="2", data=Small(2, 3)) MyModel.objects.create(name="2", data=Small(2, 3))
self.assertQuerysetEqual( self.assertQuerysetEqual(
MyModel.objects.all(), [ MyModel.objects.all(), [
@ -90,3 +92,15 @@ class CustomField(TestCase):
o = OtherModel.objects.get() o = OtherModel.objects.get()
self.assertEqual(o.data.first, "a") self.assertEqual(o.data.first, "a")
self.assertEqual(o.data.second, "b") self.assertEqual(o.data.second, "b")
def test_subfieldbase_plays_nice_with_module_inspect(self):
"""
Custom fields should play nice with python standard module inspect.
http://users.rcn.com/python/download/Descriptor.htm#properties
"""
# Even when looking for totally different properties, SubfieldBase's
# non property like behaviour made inspect crash. Refs #12568.
data = dict(inspect.getmembers(MyModel))
self.assertIn('__module__', data)
self.assertEqual(data['__module__'], 'field_subclassing.models')