From 50633e7353694ff54f14b04469be3792f286182f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anssi=20K=C3=A4=C3=A4ri=C3=A4inen?= Date: Wed, 18 Sep 2013 09:56:05 +0300 Subject: [PATCH] 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. --- django/db/models/fields/subclassing.py | 2 +- tests/field_subclassing/tests.py | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/django/db/models/fields/subclassing.py b/django/db/models/fields/subclassing.py index 591adb7121..2faf56fc00 100644 --- a/django/db/models/fields/subclassing.py +++ b/django/db/models/fields/subclassing.py @@ -30,7 +30,7 @@ class Creator(object): def __get__(self, obj, type=None): if obj is None: - raise AttributeError('Can only be accessed via an instance.') + return self return obj.__dict__[self.field.name] def __set__(self, obj, value): diff --git a/tests/field_subclassing/tests.py b/tests/field_subclassing/tests.py index d3b4d9e527..3045f7d226 100644 --- a/tests/field_subclassing/tests.py +++ b/tests/field_subclassing/tests.py @@ -1,5 +1,7 @@ from __future__ import unicode_literals +import inspect + from django.core import serializers from django.test import TestCase @@ -74,7 +76,7 @@ class CustomField(TestCase): m.delete() 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( MyModel.objects.all(), [ @@ -90,3 +92,15 @@ class CustomField(TestCase): o = OtherModel.objects.get() self.assertEqual(o.data.first, "a") 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')