我有一个非常基本的“地方”模式:
from django.db import models
class Place(models.Model):
name = models.CharField(max_length=100)
def __unicode__(self):
return self.name
我正在尝试使用该应用进行审核:https://github.com/diefenbach/django-reviews
当尝试将评论添加到某个Place对象时,在应用程序的视图https://github.com/diefenbach/django-reviews/blob/master/reviews/views.py中,它会达到te'save'方法并实际将记录保存在数据库中。但是它会引发以下TypeError:
Environment:
Request Method: POST
Request URL: http://ec2-54-77-118-44.eu-west-1.compute.amazonaws.com:5000/reviews/add/11/1
Django Version: 1.6.5
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'south',
'places',
'reviews')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/home/ubuntu/webapps/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
199. response = middleware_method(request, response)
File "/home/ubuntu/webapps/local/lib/python2.7/site-packages/django/contrib/sessions/middleware.py" in process_response
38. request.session.save()
File "/home/ubuntu/webapps/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/ubuntu/webapps/local/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py" in encode
87. serialized = self.serializer().dumps(session_dict)
File "/home/ubuntu/webapps/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 /reviews/add/11/1
Exception Value: <Place: Teteria India> is not JSON serializable
我不明白为什么如果我仍然没有使用任何AJAX或JSON,这个错误就会引发,并且据我所知,评论应用程序的观点也没有。它与表格有什么关系吗?
无论如何,只有一个CharField的模型不是JSON可序列化的怎么样?我是否必须提供序列化?
谢谢!
答案 0 :(得分:0)
在第148行中,它将object
存储在会话
request.session["last-rated-object"] = object
然后执行重定向到reviews_thank_you的视图
def thank_you(request, template_name="reviews/thank_you.html"):
"""Displays a thank you page.
"""
if request.session.has_key("last-rated-object"):
object = request.session.get("last-rated-object")
del request.session["last-rated-object"]
else:
object = None
return render_to_response(template_name, RequestContext(request, {
"object" : object,
}))
它返回object
而没有序列化它,我认为这是问题所在。您可以编辑该视图以返回object
的序列化版本吗?