【发布时间】:2016-03-23 00:36:49
【问题描述】:
我通过 Django restframework 中的 POST 请求获得以下数据。 我需要序列化这个数据,这个数据包含多个模型的数据。
data={'admin-1':
{'first_name':'john'
,'last_name':'white'
,'job_title':'CEO'
,'email':'test1@gmail.com'
},
'admin-2':
{'first_name':'lisa'
,'last_name':'markel'
,'job_title':'CEO'
,'email':'test2@gmail.com'
},
'company-detail':{'description':'We are a renowned engineering company'
,'size':'1-10'
,'industry':'Engineering'
,'url':'http://try.com'
,'logo':''
,'addr1':'1280 wick ter'
,'addr2':'1600'
,'city':'rkville'
,'state':'md'
,'zip_cd':'12000'
,'phone_number_1':'408-393-254'
,'phone_number_2':'408-393-221'}
r = requests.post('http://127.0.0.1:8000/api/create-company-profile/',data=data)
print r.status_code
print r.text
这里是 CreateAPI 视图 -
class CompanyCreateApiView(CreateAPIView):
def post(self, request, *args, **kwargs):
print 'request ==', request
print 'request.data == ', request.data['admin-2']
import json
print json.loads(request.data)
data=json.dumps({'status':'success'})
return Response(data, status=status.HTTP_200_OK)
我基本上需要反序列化数据但得到这个错误。
请求 == request.data == job_title 内部服务器错误:/api/create-company-profile/ 回溯(最近一次通话最后): 文件“/Users/prem/.virtualenvs/ghost/lib/python2.7/site-packages/django/core/handlers/base.py”, 第 111 行,在 get_response 中 响应 = Wrapped_callback(request, *callback_args, **callback_kwargs) 文件“/Users/prem/.virtualenvs/ghost/lib/python2.7/site-packages/django/views/decorators/csrf.py”, 第 57 行,在 Wrapped_view 中 返回 view_func(*args, **kwargs) 文件“/Users/prem/.virtualenvs/ghost/lib/python2.7/site-packages/django/views/generic/base.py”, 第 69 行,在视图中 return self.dispatch(request, *args, **kwargs) 文件“/Users/prem/.virtualenvs/ghost/lib/python2.7/site-packages/rest_framework/views.py”, 第 452 行,正在调度中 响应 = self.handle_exception(exc) 文件“/Users/prem/.virtualenvs/ghost/lib/python2.7/site-packages/rest_framework/views.py”, 第 449 行,正在调度中 响应 = 处理程序(请求,*args,**kwargs) 文件“/Users/prem/Documents/Ghost/positionmatch-new/menkes-server-master/menkesserver/human_resources/views.py”, 第 81 行,在帖子中 打印 json.loads(request.data) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/init.py”, 第 338 行,在负载中 返回 _default_decoder.decode(s) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py”, 第 365 行,在解码中 obj, end = self.raw_decode(s, idx=_w(s, 0).end()) TypeError: 预期的字符串或缓冲区
【问题讨论】:
标签: python django serialization django-rest-framework