Convert field type from raw bytes to native python type

        :param cast_to: native python type to cast to
        :type cast_to: a type object (one of bytes, int, unicode (str for py3k))
        :param value: raw value from the database
        :type value: bytes

        :return: converted value
        :rtype: value of native python type (one of bytes, int, unicode (str for py3k))

def _cast_field(self, cast_to, value):
        """
        Convert field type from raw bytes to native python type

        :param cast_to: native python type to cast to
        :type cast_to: a type object (one of bytes, int, unicode (str for py3k))
        :param value: raw value from the database
        :type value: bytes

        :return: converted value
        :rtype: value of native python type (one of bytes, int, unicode (str for py3k))
        """
        if cast_to in (int, long, str):
            return cast_to(value)
        elif cast_to == unicode:
            try:
                value = value.decode(self.charset, self.errors)
            except UnicodeEncodeError, e:
                raise InvalidData("Error encoding unicode value '%s': %s" % (repr(value), e))

            return value
        elif cast_to in (any, bytes):
            return value
        else:
            raise TypeError("Invalid field type %s" % (cast_to))