Ticket #10728: 10728.patch

File 10728.patch, 5.3 KB (added by George Sakkis, 14 years ago)

Added the new in Django 1.2 connection argument in get_db_prep_save()

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

    diff --git a/django/db/models/fields/subclassing.py b/django/db/models/fields/subclassing.py
    a b  
    7676    A metaclass for custom Field subclasses. This ensures the model's attribute
    7777    has the descriptor protocol attached to it.
    7878    """
    79     def __new__(cls, base, name, attrs):
    80         new_class = super(SubfieldBase, cls).__new__(cls, base, name, attrs)
    81         new_class.contribute_to_class = make_contrib(
    82                 attrs.get('contribute_to_class'))
    83         return new_class
     79    def __init__(cls, name, base, attrs):
     80        def contribute_to_class(self, model_cls, name):
     81            super(cls, self).contribute_to_class(model_cls, name)
     82            setattr(model_cls, name, Creator(self))
     83        cls.contribute_to_class = contribute_to_class
    8484
    8585class Creator(object):
    8686    """
     
    9696
    9797    def __set__(self, obj, value):
    9898        obj.__dict__[self.field.name] = self.field.to_python(value)
    99 
    100 def make_contrib(func=None):
    101     """
    102     Returns a suitable contribute_to_class() method for the Field subclass.
    103 
    104     If 'func' is passed in, it is the existing contribute_to_class() method on
    105     the subclass and it is called before anything else. It is assumed in this
    106     case that the existing contribute_to_class() calls all the necessary
    107     superclass methods.
    108     """
    109     def contribute_to_class(self, cls, name):
    110         if func:
    111             func(self, cls, name)
    112         else:
    113             super(self.__class__, self).contribute_to_class(cls, name)
    114         setattr(cls, self.name, Creator(self))
    115 
    116     return contribute_to_class
  • tests/modeltests/field_subclassing/fields.py

    diff --git a/tests/modeltests/field_subclassing/fields.py b/tests/modeltests/field_subclassing/fields.py
    a b  
    4040            return value
    4141        return Small(value[0], value[1])
    4242
    43     def get_db_prep_save(self, value):
     43    def get_db_prep_save(self, value, connection):
    4444        return unicode(value)
    4545
    4646    def get_prep_lookup(self, lookup_type, value):
     
    5353        raise TypeError('Invalid lookup type: %r' % lookup_type)
    5454
    5555
     56class SmallerField(SmallField):
     57    """
     58    Checks that the SubfieldBase metaclass works with inheritance.
     59    """
     60
     61
    5662class JSONField(models.TextField):
    5763    __metaclass__ = models.SubfieldBase
    5864
     
    6268    def to_python(self, value):
    6369        if not value:
    6470            return None
    65 
    6671        if isinstance(value, basestring):
    6772            value = json.loads(value)
    6873        return value
    6974
    70     def get_db_prep_save(self, value):
     75    def get_db_prep_save(self, value, connection):
    7176        if value is None:
    7277            return None
    7378        return json.dumps(value)
  • tests/modeltests/field_subclassing/models.py

    diff --git a/tests/modeltests/field_subclassing/models.py b/tests/modeltests/field_subclassing/models.py
    a b  
    55from django.db import models
    66from django.utils.encoding import force_unicode
    77
    8 from fields import Small, SmallField, JSONField
     8from fields import Small, SmallField, SmallerField, JSONField
    99
    1010
    11 class MyModel(models.Model):
     11class AbstractSmallModel(models.Model):
    1212    name = models.CharField(max_length=10)
    13     data = SmallField('small field')
     13
     14    class Meta:
     15        abstract = True
    1416
    1517    def __unicode__(self):
    1618        return force_unicode(self.name)
    1719
     20
     21class SmallModel(AbstractSmallModel):
     22    data = SmallField('small field')
     23
     24
     25try:
     26    class SmallerModel(AbstractSmallModel):
     27        data = SmallerField('small field')
     28except RuntimeError: # Ticket #10728
     29    SmallerModel = None
     30
     31
    1832class DataModel(models.Model):
    1933    data = JSONField()
  • tests/modeltests/field_subclassing/tests.py

    diff --git a/tests/modeltests/field_subclassing/tests.py b/tests/modeltests/field_subclassing/tests.py
    a b  
    22from django.test import TestCase
    33
    44from fields import Small
    5 from models import DataModel, MyModel
     5from models import DataModel, SmallModel, SmallerModel
    66
    77
    88class CustomField(TestCase):
     
    2222        self.assertTrue(isinstance(d.data, list))
    2323        self.assertEqual(d.data, [1, 2, 3])
    2424
    25     def test_custom_field(self):
     25    def test_custom_field(self, MyModel=SmallModel):
    2626        # Creating a model with custom fields is done as per normal.
    2727        s = Small(1, 2)
    2828        self.assertEqual(str(s), "12")
     
    5555
    5656        # Serialization works, too.
    5757        stream = serializers.serialize("json", MyModel.objects.all())
    58         self.assertEqual(stream, '[{"pk": 1, "model": "field_subclassing.mymodel", "fields": {"data": "12", "name": "m"}}]')
     58        self.assertEqual(stream, '[{"pk": 1, "model": "field_subclassing.%s", '
     59                                 '"fields": {"data": "12", "name": "m"}}]'
     60                                 % MyModel.__name__.lower())
    5961
    6062        obj = list(serializers.deserialize("json", stream))[0]
    6163        self.assertEqual(obj.object, m)
     
    7375            ],
    7476            lambda m: str(m.data)
    7577        )
     78
     79    def test_custom_field_subclass(self):
     80        if not SmallerModel:
     81            self.fail("Couldn't subclass SubfieldBase field")
     82        else:
     83            self.test_custom_field(SmallerModel)
     84
Back to Top