web-dev-qa-db-ja.com

djangoオブジェクトはJSONシリアル化可能エラーではありませんDjangoを1.6.5にアップグレードした後

Djangoアプリは、1.4.2バージョンで実行され、完全に正常に動作していましたが、最近、Django 1.6.5に更新しました。そして、以下のようないくつかの奇妙なエラーに直面しています

実際、サイト機能のユーザー/クライアント登録プロセス中にこれを取得しています

Request URL:    http://example.com/client/registration/
Django Version:     1.6.5
Exception Type:     TypeError
Exception Value:    <Client: test one> is not JSON serializable
Exception Location:     /usr/lib/python2.7/json/encoder.py in default, line 184
Python Executable:  /home/user/.virtualenvs/test_proj/bin/python
Python Version:     2.7.5

トレースバック

Traceback:
File "/home/user/.virtualenvs/test_proj/local/lib/python2.7/site-packages/Django/core/handlers/base.py" in get_response
  199.                 response = middleware_method(request, response)
File "/home/user/.virtualenvs/test_proj/local/lib/python2.7/site-packages/Django/contrib/sessions/middleware.py" in process_response
  38.                     request.session.save()
File "/home/user/.virtualenvs/test_proj/local/lib/python2.7/site-packages/Django/contrib/sessions/backends/db.py" in save
  57.             session_data=self.encode(self._get_session(no_load=must_create)),
File "/home/user/.virtualenvs/test_proj/local/lib/python2.7/site-packages/Django/contrib/sessions/backends/base.py" in encode
  87.         serialized = self.serializer().dumps(session_dict)
File "/home/user/.virtualenvs/test_proj/local/lib/python2.7/site-packages/Django/core/signing.py" in dumps
  88.         return json.dumps(obj, separators=(',', ':')).encode('latin-1')
File "/usr/lib/python2.7/json/__init__.py" in dumps
  250.         sort_keys=sort_keys, **kw).encode(obj)
File "/usr/lib/python2.7/json/encoder.py" in encode
  207.         chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py" in iterencode
  270.         return _iterencode(o, 0)
File "/usr/lib/python2.7/json/encoder.py" in default
  184.         raise TypeError(repr(o) + " is not JSON serializable")

Exception Type: TypeError at /client/registration/
Exception Value: <Client: test one> is not JSON serializable

更新後に上記のjsonエラーが表示される理由と、いくつかのモデルでカスタマイズされたjsonフィールドを以下のように使用している理由について混乱しています。

proj/utils.py

from Django.db import models
from Django.utils import simplejson as json
from Django.core.serializers.json import DjangoJSONEncoder


class JSONField(models.TextField):
    '''JSONField is a generic textfield that neatly serializes/unserializes
    JSON objects seamlessly'''

    # Used so to_python() is called
    __metaclass__ = models.SubfieldBase

    def to_python(self, value):
        '''Convert our string value to JSON after we load it from the DB'''
        if value == '':
            return None
        try:
            if isinstance(value, basestring):
                return json.loads(value)
        except ValueError:
            pass
        return value

    def get_db_prep_save(self, value, connection=None):
        '''Convert our JSON object to a string before we save'''
        if not value or value == '':
            return None
        if isinstance(value, (dict, list)):
            value = json.dumps(value, mimetype="application/json")
        return super(JSONField, self).get_db_prep_save(value, connection=connection)

from south.modelsinspector import add_introspection_rules
add_introspection_rules([], ["^proj\.util\.jsonfield\.JSONField"])

settings.py

SERIALIZATION_MODULES = {
     'custom_json': 'proj.util.json_serializer',
        }

json_serializer.py

from Django.core.serializers.json import Serializer as JSONSerializer
from Django.utils.encoding import is_protected_type

# JSONFields that are normally incorrectly serialized as strings
json_fields = ['field_1', 'field_2']


class Serializer(JSONSerializer):
    """
    A fix on JSONSerializer in order to prevent stringifying JSONField data.
    """
    def handle_field(self, obj, field):
        value = field._get_val_from_obj(obj)
        # Protected types (i.e., primitives like None, numbers, dates,
        # and Decimals) are passed through as is. All other values are
        # converted to string first.
        if is_protected_type(value) or field.name in json_fields:
            self._current[field.name] = value
        else:
            self._current[field.name] = field.value_to_string(obj)

では、上記のエラーを解決する方法は?誰かが私にエラーを引き起こす原因の説明を教えてもらえますか?

Django 1.6は、シリアライザーをpickleからjsonに変更しました。 pickleは、jsonができないことをシリアル化できます。

settings.pyの-​​ SESSION_SERIALIZER の値を変更して、バージョン1.6より前のDjangoからの動作を取り戻すことができます。

SESSION_SERIALIZER = 'Django.contrib.sessions.serializers.PickleSerializer'

ドキュメントの セッションのシリアル化 について読むことをお勧めします。

44
Matthias

この行をsettings.pyに設定すると、Django 1.6バージョンにアップグレードしたときにエラーがクリアされます

SESSION_SERIALIZER = 'Django.contrib.sessions.serializers.PickleSerializer'

トレースバックを分析した後、JSONEncoderはClientモデルのインスタンスをシリアル化できないようです。一般に、jsonまたはsimplejsonライブラリを使用して、他のモデル(Many2ManyFieldなど)に関連するモデルをシリアル化しようとすると、このようなエラーが発生します。

これを参照してください https://docs.djangoproject.com/en/dev/topics/serialization/ 、場合によってはDjangoRestFrameworkなどのサードパーティパッケージを使用することもできますニーズ。