Ticket #10728: 0001-Allow-subclassing-field-types-that-use-the-SubfieldB.patch

File 0001-Allow-subclassing-field-types-that-use-the-SubfieldB.patch, 3.1 KB (added by Gabriel, 14 years ago)

Both patches squashed, and a nicer test failure mode. Tested on 1.2.0a1 trunk.

  • django/db/models/fields/subclassing.py

    From b93e44c8b0c875c4407a00a9b02c92540e25e094 Mon Sep 17 00:00:00 2001
    From: Gabriel <g2p.code@gmail.com>
    Date: Sat, 4 Apr 2009 02:22:01 +0200
    Subject: [PATCH] Allow subclassing field types that use the SubfieldBase metaclass.
    
    Includes a test that used to fail but now doesn't.
    ---
     django/db/models/fields/subclassing.py       |    6 ++--
     tests/modeltests/field_subclassing/models.py |   30 ++++++++++++++++++++++++++
     2 files changed, 33 insertions(+), 3 deletions(-)
    
    diff --git a/django/db/models/fields/subclassing.py b/django/db/models/fields/subclassing.py
    index bd11675..ea100fd 100644
    a b class SubfieldBase(LegacyConnection):  
    7979    def __new__(cls, base, name, attrs):
    8080        new_class = super(SubfieldBase, cls).__new__(cls, base, name, attrs)
    8181        new_class.contribute_to_class = make_contrib(
    82                 attrs.get('contribute_to_class'))
     82                attrs.get('contribute_to_class'), new_class)
    8383        return new_class
    8484
    8585class Creator(object):
    class Creator(object):  
    9797    def __set__(self, obj, value):
    9898        obj.__dict__[self.field.name] = self.field.to_python(value)
    9999
    100 def make_contrib(func=None):
     100def make_contrib(func, field_class):
    101101    """
    102102    Returns a suitable contribute_to_class() method for the Field subclass.
    103103
    def make_contrib(func=None):  
    110110        if func:
    111111            func(self, cls, name)
    112112        else:
    113             super(self.__class__, self).contribute_to_class(cls, name)
     113            super(field_class, self).contribute_to_class(cls, name)
    114114        setattr(cls, self.name, Creator(self))
    115115
    116116    return contribute_to_class
  • tests/modeltests/field_subclassing/models.py

    diff --git a/tests/modeltests/field_subclassing/models.py b/tests/modeltests/field_subclassing/models.py
    index c776146..09faf1c 100644
    a b class SmallField(models.Field):  
    5353            return []
    5454        raise FieldError('Invalid lookup type: %r' % lookup_type)
    5555
     56class OtherField(SmallField):
     57    """
     58    Check the SubfieldBase metaclass works with inheritance.
     59    """
     60
     61    pass
     62
     63def make_othermodel():
     64    """
     65    Isolate OtherModel class definition.
     66
     67    When the test fails, it fails at class definition with a long
     68    stack trace. This confuses test discovery, so wrap it in a function.
     69    """
     70
     71    try:
     72        class OtherModel(models.Model):
     73            other_data = OtherField(default='example')
     74    except RuntimeError: # Maximum recursion depth
     75        raise RuntimeError("Couldn't subclass field")
     76    else:
     77        return OtherModel
     78
    5679class MyModel(models.Model):
    5780    name = models.CharField(max_length=10)
    5881    data = SmallField('small field')
    FieldError: Invalid lookup type: 'lt'  
    102125>>> obj.object == m
    103126True
    104127
     128# Test custom field subclassing.
     129>>> OtherModel = make_othermodel()
     130>>> om = OtherModel()
     131>>> om.other_data = 'plop'
     132>>> str(om.other_data)
     133"pl"
     134
    105135# Test retrieving custom field data
    106136>>> m.delete()
    107137>>> m1 = MyModel(name="1", data=Small(1, 2))
Back to Top