Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.2.X] Fixed #10728 -- corrected subclassing of Fields who use the S…

…ubfieldBase metaclass. Thanks to G2P and George Sakkis for their work on the patch. Backport of [14443].

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14444 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 6f30440b7291deac7e7de0c0e422f2bcc89a3c6d 1 parent a25718c
Alex Gaynor authored November 02, 2010
7  django/db/models/fields/subclassing.py
@@ -79,7 +79,8 @@ class SubfieldBase(LegacyConnection):
79 79
     def __new__(cls, base, name, attrs):
80 80
         new_class = super(SubfieldBase, cls).__new__(cls, base, name, attrs)
81 81
         new_class.contribute_to_class = make_contrib(
82  
-                attrs.get('contribute_to_class'))
  82
+            new_class, attrs.get('contribute_to_class')
  83
+        )
83 84
         return new_class
84 85
 
85 86
 class Creator(object):
@@ -97,7 +98,7 @@ def __get__(self, obj, type=None):
97 98
     def __set__(self, obj, value):
98 99
         obj.__dict__[self.field.name] = self.field.to_python(value)
99 100
 
100  
-def make_contrib(func=None):
  101
+def make_contrib(superclass, func=None):
101 102
     """
102 103
     Returns a suitable contribute_to_class() method for the Field subclass.
103 104
 
@@ -110,7 +111,7 @@ def contribute_to_class(self, cls, name):
110 111
         if func:
111 112
             func(self, cls, name)
112 113
         else:
113  
-            super(self.__class__, self).contribute_to_class(cls, name)
  114
+            super(superclass, self).contribute_to_class(cls, name)
114 115
         setattr(cls, self.name, Creator(self))
115 116
 
116 117
     return contribute_to_class
3  tests/modeltests/field_subclassing/fields.py
@@ -50,6 +50,9 @@ def get_prep_lookup(self, lookup_type, value):
50 50
             return []
51 51
         raise TypeError('Invalid lookup type: %r' % lookup_type)
52 52
 
  53
+class SmallerField(SmallField):
  54
+    pass
  55
+
53 56
 
54 57
 class JSONField(models.TextField):
55 58
     __metaclass__ = models.SubfieldBase
5  tests/modeltests/field_subclassing/models.py
@@ -5,7 +5,7 @@
5 5
 from django.db import models
6 6
 from django.utils.encoding import force_unicode
7 7
 
8  
-from fields import Small, SmallField, JSONField
  8
+from fields import Small, SmallField, SmallerField, JSONField
9 9
 
10 10
 
11 11
 class MyModel(models.Model):
@@ -15,5 +15,8 @@ class MyModel(models.Model):
15 15
     def __unicode__(self):
16 16
         return force_unicode(self.name)
17 17
 
  18
+class OtherModel(models.Model):
  19
+    data = SmallerField()
  20
+
18 21
 class DataModel(models.Model):
19 22
     data = JSONField()
8  tests/modeltests/field_subclassing/tests.py
@@ -2,7 +2,7 @@
2 2
 from django.test import TestCase
3 3
 
4 4
 from fields import Small
5  
-from models import DataModel, MyModel
  5
+from models import DataModel, MyModel, OtherModel
6 6
 
7 7
 
8 8
 class CustomField(TestCase):
@@ -73,3 +73,9 @@ def test_custom_field(self):
73 73
             ],
74 74
             lambda m: str(m.data)
75 75
         )
  76
+
  77
+    def test_field_subclassing(self):
  78
+        o = OtherModel.objects.create(data=Small("a", "b"))
  79
+        o = OtherModel.objects.get()
  80
+        self.assertEqual(o.data.first, "a")
  81
+        self.assertEqual(o.data.second, "b")

0 notes on commit 6f30440

Please sign in to comment.
Something went wrong with that request. Please try again.