Code

Ticket #12568: subfieldbase-20130917.diff

File subfieldbase-20130917.diff, 1.6 KB (added by supervacuo, 10 months ago)

Rebased against git master (5be56d0)

Line 
1diff --git a/django/db/models/fields/subclassing.py b/django/db/models/fields/subclassing.py
2index 591adb7..43c3449 100644
3--- a/django/db/models/fields/subclassing.py
4+++ b/django/db/models/fields/subclassing.py
5@@ -30,7 +30,7 @@ class Creator(object):
6 
7     def __get__(self, obj, type=None):
8         if obj is None:
9-            raise AttributeError('Can only be accessed via an instance.')
10+             return self
11         return obj.__dict__[self.field.name]
12 
13     def __set__(self, obj, value):
14diff --git a/tests/field_subclassing/tests.py b/tests/field_subclassing/tests.py
15index d3b4d9e..176d9bc 100644
16--- a/tests/field_subclassing/tests.py
17+++ b/tests/field_subclassing/tests.py
18@@ -1,5 +1,7 @@
19 from __future__ import unicode_literals
20 
21+import inspect
22+
23 from django.core import serializers
24 from django.test import TestCase
25 
26@@ -90,3 +92,19 @@ class CustomField(TestCase):
27         o = OtherModel.objects.get()
28         self.assertEqual(o.data.first, "a")
29         self.assertEqual(o.data.second, "b")
30+
31+    def test_subfieldbase_plays_nice_with_module_inspect(self):
32+        """
33+        Custom fields should play nice with python standard module inspect.
34+
35+        http://users.rcn.com/python/download/Descriptor.htm#properties
36+        """
37+        # even when looking for totally different properties, SubfieldBase it's
38+        # non property like behaviour makes inspect crash.
39+        strings = inspect.getmembers(MyModel, lambda x: type(x) is str)
40+        known_strings = [('__module__', 'field_subclassing.models')]
41+
42+        self.assertEqual(strings, known_strings)
43+
44+        # should also not raise an exception
45+        inspect.getmembers(MyModel)