Ticket #1987: django-utf8string.patch

File django-utf8string.patch, 2.5 KB (added by anonymous, 18 years ago)
  • django/db/models/base.py

     
    110110        if kwargs:
    111111            raise TypeError, "'%s' is an invalid keyword argument for this function" % kwargs.keys()[0]
    112112        for i, arg in enumerate(args):
    113             setattr(self, self._meta.fields[i].attname, arg)
     113            f = self._meta.fields[i]
     114            setattr(self, f.attname, f.get_db_post_load(arg))
    114115        dispatcher.send(signal=signals.post_init, sender=self.__class__, instance=self)
    115116
    116117    def add_to_class(cls, name, value):
  • django/db/models/fields/__init__.py

     
    156156        "Returns field's value just before saving."
    157157        return value
    158158
     159    def get_db_post_load(self, value):
     160        "Returns field's value after loading from a database onto memory."
     161        return value
     162
    159163    def get_db_prep_save(self, value):
    160164        "Returns field's value prepared for saving into a database."
    161165        return value
     
    385389                raise validators.ValidationError, gettext_lazy("This field cannot be null.")
    386390        return str(value)
    387391
     392# for handling of "utf-8" encoding (hironobu@nerv.or.jp,20060524)
     393class UTF8StringField(CharField):
     394    def get_manipulator_field_objs(self):
     395        return [forms.TextField]
     396
     397    def get_internal_type(self):
     398        return "CharField"
     399
     400    def get_db_post_load(self, value):
     401        if value is not None:
     402            value = value.decode('utf-8')
     403        return Field.get_db_post_load(self, value)
     404
     405    def get_db_prep_lookup(self, lookup_type, value):
     406        if value is not None:
     407            value = value.decode('utf-8')
     408        return Field.get_db_prep_lookup(self, lookup_type, value)
     409
     410    def to_python(self, value):
     411        if isinstance(value, basestring):
     412            return value.decode('utf-8')
     413        if value is None:
     414            if self.null:
     415                return value
     416            else:
     417                raise validators.ValidationError, gettext_lazy("This field cannot be null.")
     418        return value.decode('utf-8')
     419
    388420# TODO: Maybe move this into contrib, because it's specialized.
    389421class CommaSeparatedIntegerField(CharField):
    390422    def get_manipulator_field_objs(self):
Back to Top