Ticket #3148: django-3148.diff

File django-3148.diff, 3.8 KB (added by Daniel Hahler, 10 years ago)

Slightly updated patch

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

     
    9797            self.creation_counter = Field.creation_counter
    9898            Field.creation_counter += 1
    9999
     100        self._property = {} #  stores kwargs for property ('fget', 'fset', 'fdel')
     101
    100102    def __cmp__(self, other):
    101103        # This is needed because bisect does not take a comparison function.
    102104        return cmp(self.creation_counter, other.creation_counter)
     
    148150        return self._unique or self.primary_key
    149151    unique = property(unique)
    150152
     153    def setter(self, f):
     154        self._property['fset'] = f
     155        return f
     156
     157    def getter(self, f):
     158        self._property['fget'] = f
     159        return f
     160
     161    def deleter(self, f):
     162        self._property['fdel'] = f
     163        return f
     164
    151165    def set_attributes_from_name(self, name):
    152166        self.name = name
    153167        self.attname, self.column = self.get_attname_column()
     
    160174        if self.choices:
    161175            setattr(cls, 'get_%s_display' % self.name, curry(cls._get_FIELD_display, field=self))
    162176
     177        if self._property:
     178            setattr(cls, name, property(**self._property))
     179            del(self._property)
     180
     181
    163182    def get_attname(self):
    164183        return self.name
    165184
  • tests/regressiontests/model_fields/tests.py

     
    77from django.core.exceptions import ValidationError
    88
    99from models import Foo, Bar, Whiz, BigD, BigS, Image
     10from models import FooGetSet
    1011
    1112try:
    1213    from decimal import Decimal
     
    144145        bs = BigS.objects.create(s = 'slug'*50)
    145146        bs = BigS.objects.get(pk=bs.pk)
    146147        self.assertEqual(bs.s, 'slug'*50)
     148
     149class FieldProperties(django.test.TestCase):
     150    def test_get_set(self):
     151        """
     152        Test basic getter and setter functionality of a SlugField
     153        """
     154        f = FooGetSet( s = "test" )
     155        self.assertEqual(f.s, "test")
     156        f.s = "test2"
     157        self.assertEqual(f.s, "test2")
     158
     159        def set_non_acceptable():
     160            f.s = "not_acceptable"
     161        self.assertRaises(ValueError, set_non_acceptable)
     162        self.assertEqual(f.s, "test2")
     163
     164        f.save()
     165        f = FooGetSet.objects.get(pk=f.pk)
     166        self.assertEqual(f.s, "test2")
     167
     168        self.assertEqual(f.file.__class__.__name__, "FieldFile")
     169
  • tests/regressiontests/model_fields/models.py

     
    1717from django.db import models
    1818from django.db.models.fields.files import ImageFieldFile, ImageField
    1919
     20temp_storage_location = tempfile.mkdtemp()
     21temp_storage = FileSystemStorage(location=temp_storage_location)
    2022
    2123class Foo(models.Model):
    2224    a = models.CharField(max_length=10)
     
    5153class BigS(models.Model):
    5254    s = models.SlugField(max_length=255)
    5355
     56class FooGetSet(models.Model):
     57    s = models.SlugField(max_length=255)
     58    file = models.FileField(storage=temp_storage, upload_to='tests')
    5459
     60    @s.getter
     61    def get_s(self):
     62        return self._s
     63    @s.setter
     64    def set_s(self, value):
     65        if value is "not_acceptable":
     66            raise ValueError("not acceptable")
     67        self._s = value
     68
     69    @file.getter
     70    def get_file(self):
     71        assert False
     72    @file.setter
     73    def set_file(self, value):
     74        assert False
     75
    5576###############################################################################
    5677# ImageField
    5778
Back to Top