将django升级到1.6.5后,django对象不是JSON序列化错误 [英] django object is not JSON serializable error after upgrading django to 1.6.5

查看:195
本文介绍了将django升级到1.6.5后,django对象不是JSON序列化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个django应用程序运行在 1.4.2 版本,工作完全正常,但最近我更新到django 1.6.5

I have a django app which was running on 1.4.2 version and working completely fine, but recently i updated it to django 1.6.5 and facing some wierd errors like below

其实我在网站功能的用户/客户端注册过程中得到这个错误

Actually i am getting this during user/client registration process in my site functionality

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字段,如下所示

I am confused on why the above json error has been appearing after updation and by the way i am using a customized json field in some of my models as below

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)

所以如何解决上述错误?有人可以给我一个解释导致错误的情况?

So how to solve the above error ? can some one give me an explanation of what happening to cause the error ?

推荐答案

Django 1.6将序列化器从pickle更改为json,pickle可以序列化json不能的东西。

Django 1.6 changed the serializer from pickle to json. pickle can serialize things that json can't.

您可以更改 SESSION_SERIALIZER 的值在您的 settings.py 可以在版本1.6之前从Django获取行为。

You can change the value of SESSION_SERIALIZER in your settings.py to get back the behaviour from Django before version 1.6.

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

您可能想要阅读关于会话序列化。

这篇关于将django升级到1.6.5后,django对象不是JSON序列化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆