Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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.
  • Loading branch information...
commit 50633e7353694ff54f14b04469be3792f286182f 1 parent 5be56d0
@akaariai akaariai authored
View
2  django/db/models/fields/subclassing.py
@@ -30,7 +30,7 @@ def __init__(self, field):
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):
View
16 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 @@ def test_custom_field(self):
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 @@ def test_field_subclassing(self):
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')
Please sign in to comment.
Something went wrong with that request. Please try again.