Help us understand the problem. What is going on with this article?

【django version up】TextFiledを保存するときのstringへの変換の仕方が変わった

背景

Djangoのバージョンを1.11.1から2.2へあげると、バイト文字列で保存したTextFieldsの値が取り出した時に、
b'...'と皮まで保存されるようになっていた。

結論

  • decodeを使ってstring型に変換していたのがstr()を使ってstring型へ変更するようになっていた。
  • str()を使うと改行文字がエスケープされたり、b''まで文字列に含まれたりする。

参考

version 1.11.1

django.db.models.fields.py
from django.utils.encoding import (
    force_bytes, force_text, python_2_unicode_compatible, smart_text,
)

class TextField(Field):
    description = _("Text")

    def to_python(self, value):
        if isinstance(value, six.string_types) or value is None:
            return value
        return force_text(value)
django.utils.encoding.py
def force_text(s, encoding='utf-8', strings_only=False, errors='strict'):
    """
    Similar to smart_text, except that lazy instances are resolved to
    strings, rather than kept as lazy objects.

    If strings_only is True, don't convert (some) non-string-like objects.
    """
    # Handle the common case first for performance reasons.
    if issubclass(type(s), six.text_type):
        return s
    if strings_only and is_protected_type(s):
        return s
    try:
        if not issubclass(type(s), six.string_types):
            if six.PY3:
                if isinstance(s, bytes):
                    s = six.text_type(s, encoding, errors)
                else:
                    s = six.text_type(s)
            elif hasattr(s, '__unicode__'):
                s = six.text_type(s)
            else:
                s = six.text_type(bytes(s), encoding, errors)
        else:
            # Note: We use .decode() here, instead of six.text_type(s, encoding,
            # errors), so that if s is a SafeBytes, it ends up being a
            # SafeText at the end.
            s = s.decode(encoding, errors)
    except UnicodeDecodeError as e:
        if not isinstance(s, Exception):
            raise DjangoUnicodeDecodeError(s, *e.args)
        else:
            # If we get to here, the caller has passed in an Exception
            # subclass populated with non-ASCII bytestring data without a
            # working unicode method. Try to handle this without raising a
            # further exception by individually forcing the exception args
            # to unicode.
            s = ' '.join(force_text(arg, encoding, strings_only, errors)
                         for arg in s)
    return s

version 2.2

django.db.models.fields.py
class TextField(Field):
    description = _("Text")

    def to_python(self, value):
        if isinstance(value, str) or value is None:
            return value
        return str(value)
nakamumu
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした