Ticket #3982: coerce.diff

File coerce.diff, 782 bytes (added by Marty Alchin <gulopine@…>, 8 years ago)

Adds support for a coerce Field method to handle coercion

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

     
    140140        cls._meta.add_field(self)
    141141        if self.choices:
    142142            setattr(cls, 'get_%s_display' % self.name, curry(cls._get_FIELD_display, field=self))
     143        if hasattr(self, 'coerce'):
     144            dispatcher.connect(self._force_coercion, signal=signals.post_init, sender=cls)
    143145
     146    def _force_coercion(self, sender, instance, signal, *args, **kwargs):
     147        value = self.coerce(getattr(instance, self.attname))
     148        setattr(instance, self.attname, value)
     149
    144150    def get_attname(self):
    145151        return self.name
    146152
Back to Top