Ticket #3148: 3148.4.diff

File 3148.4.diff, 3.6 KB (added by Marc Fargas, 11 years ago)

Updated patch (merge conflict resolved) to latest trunk.

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

    diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
    index 8803375..b4947e8 100644
    a b class Field(object): 
    8989            editable=True, serialize=True, prepopulate_from=None,
    9090            unique_for_date=None, unique_for_month=None, unique_for_year=None,
    9191            validator_list=None, choices=None, radio_admin=None, help_text='',
    92             db_column=None, db_tablespace=None, auto_created=False):
     92            db_column=None, db_tablespace=None, auto_created=False, use_property=None):
    9393        self.name = name
    9494        self.verbose_name = verbose_name
    9595        self.primary_key = primary_key
    9696        self.max_length, self.unique = max_length, unique
    9797        self.blank, self.null = blank, null
     98        self.model_property = use_property
    9899        # Oracle treats the empty string ('') as null, so coerce the null
    99100        # option whenever '' is a possible value.
    100101        if self.empty_strings_allowed and settings.DATABASE_ENGINE == 'oracle':
    class Field(object): 
    202203        cls._meta.add_field(self)
    203204        if self.choices:
    204205            setattr(cls, 'get_%s_display' % self.name, curry(cls._get_FIELD_display, field=self))
     206        if self.model_property:
     207            if len(self.model_property) < 2:
     208                raise ValueError("You must specify at least a getter and a setter method")
     209            # Create a property on ``cls`` with the methods given.
     210            setattr(cls, '%s' % self.name,
     211                property(*self.model_property))
    205212
    206213    def get_attname(self):
    207214        return self.name
  • docs/model-api.txt

    diff --git a/docs/model-api.txt b/docs/model-api.txt
    index 4ed4ede..1e1129a 100644
    a b unless you want to override the default primary-key behavior. 
    665665``primary_key=True`` implies ``blank=False``, ``null=False`` and
    666666``unique=True``. Only one primary key is allowed on an object.
    667667
     668``use_property``
     669~~~~~~~~~~~~~~~~
     670
     671It is possible to create a property around a field. It is specially usefull if
     672you want to control when the value for a field gets changed or accessed.
     673
     674The ``use_property`` option takes a tuple of the parameters that will be passed to
     675``property()`` to construct it.
     676
     677But note that at least getter and setter functions must be given.
     678
     679Example::
     680
     681    from django.db import models
     682
     683    class Person(models.Model):
     684        def _get_name(self):
     685            return self.__name
     686        def _set_name(self, value):
     687            self.__name = value
     688
     689        name = models.CharField(max_length=30, use_property=(_get_name, _set_name))
     690
    668691``radio_admin``
    669692~~~~~~~~~~~~~~~
    670693
  • tests/modeltests/properties/models.py

    diff --git a/tests/modeltests/properties/models.py b/tests/modeltests/properties/models.py
    index 5326e4e..9aa2c55 100644
    a b class Person(models.Model): 
    2020
    2121    full_name_2 = property(_get_full_name, _set_full_name)
    2222
     23class PropModel(models.Model):
     24    def _set_name(self, value):
     25        self.__name = value
     26
     27    def _get_name(self):
     28        return self.__name
     29
     30    name = models.CharField(max_length=30, use_property=(_get_name, _set_name))
     31
    2332__test__ = {'API_TESTS':"""
    2433>>> a = Person(first_name='John', last_name='Lennon')
    2534>>> a.save()
    AttributeError: can't set attribute 
    3746>>> a2.save()
    3847>>> a2.first_name
    3948'Paul'
     49
     50# Now the field properties
     51>>> b = PropModel(name='John')
     52>>> b.save()
     53>>> b.name
     54'John'
     55>>> b._get_name()
     56'John'
     57>>> b.name = 'Smith'
     58>>> b._get_name()
     59'Smith'
    4060"""}
     61
Back to Top