1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Django:第3の文字列

Last updated at Posted at 2018-10-23

使い古された話題かもですがちょっと面白いと思ったのでメモ。

##Form.fieldの中にこんなのがいた。
Form.fieldから値を取り出してjsonにしようとしたら怒られた。

TypeError: Object of type '__proxy__' is not JSON serializable

なんでそんなことしてんのってのはともかく、この**__proxy__とは何ぞってfield.__dict__**を回してみると、なんか居るね。

In [44]: for k, v in f.fields.items():
    ...:     print(k, v)
    ...:     for kk, vv in v.__dict__.items():
    ...:         print(type(vv))
    ...:         if isinstance(vv, dict):
    ...:             for kkk, vvv in vv.items():
    ...:                 print(type(vvv))
    ...:
costomer_id <django.forms.fields.CharField object at 0x0000000005BAE860>
<class 'int'>
<class 'NoneType'>
<class 'bool'>
<class 'str'>
<class 'bool'>
<class 'str'>
<class 'NoneType'>
<class 'bool'>
<class 'str'>
<class 'bool'>
<class 'NoneType'>
<class 'bool'>
<class 'dict'>
<class 'django.utils.functional.lazy.<locals>.__proxy__'>

##第3のストリング
公式ドキュメントに載ってます。はい。
Translated strings(翻訳された文字列)

Aside from strings and bytestrings, there’s a third type of string-like object you may encounter when using Django. The framework’s internationalization features introduce the concept of a “lazy translation” – a string that has been marked as translated but whose actual translation result isn’t determined until the object is used in a string. This feature is useful in cases where the translation locale is unknown until the string is used, even though the string might have originally been created when the code was first imported.

実際に文字列が使用されるまで翻訳のロケールが不明なため、宙づりにされた文字列というわけ。
ちなみに、直ちに実行中のコードで値の評価を行いたい場合は**str()**を呼べばよい。

Calling str() with the lazy translation as the argument will generate a string in the current locale.

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?