2015-12-16 81 views
1

我通过Django restframework中的POST请求获取以下数据。 我需要序列化这些数据,这些数据包含多个模型的数据。在django restframework中序列化数据

data={'admin-1': 
    {'first_name':'john' 
    ,'last_name':'white' 
    ,'job_title':'CEO' 
    ,'email':'[email protected]' 
    }, 
'admin-2': 
    {'first_name':'lisa' 
    ,'last_name':'markel' 
    ,'job_title':'CEO' 
    ,'email':'[email protected]' 
    }, 
'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 == request.data == job_title Internal Server Error: /api/create-company-profile/ Traceback (most recent call last): File "/Users/prem/.virtualenvs/ghost/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/Users/prem/.virtualenvs/ghost/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 57, in wrapped_view return view_func(*args, **kwargs) File "/Users/prem/.virtualenvs/ghost/lib/python2.7/site-packages/django/views/generic/base.py", line 69, in view return self.dispatch(request, *args, **kwargs) File "/Users/prem/.virtualenvs/ghost/lib/python2.7/site-packages/rest_framework/views.py", line 452, in dispatch response = self.handle_exception(exc) File "/Users/prem/.virtualenvs/ghost/lib/python2.7/site-packages/rest_framework/views.py", line 449, in dispatch response = handler(request, *args, **kwargs) File "/Users/prem/Documents/Ghost/positionmatch-new/menkes-server-master/menkesserver/human_resources/views.py", line 81, in post print json.loads(request.data) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/init.py", line 338, in loads return _default_decoder.decode(s) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) TypeError: expected string or buffer

回答

0

应该没有必要在视图中手动解码POST数据。只要您使用JSONParser,如parsers documentation中所述,request.data应该已经完全为您解析。

此外,它看起来像您发送到视图的请求可能不会如何行事。如果你想发送带有请求的JSON数据,你需要更明确一点。如图所示在requests documentation的例子:

>>> import json 
>>> url = 'https://api.github.com/some/endpoint' 
>>> payload = {'some': 'data'} 

>>> r = requests.post(url, data=json.dumps(payload)) 
+0

当我显示request.data,我没有得到完整的数据,多数民众赞成被张贴。它看起来像被截断,但不知道......这里是打印的数据... request.data == {u'admin-1':u'job_title',u'company-detail':u'zip_cd',u'admin-2':u'job_title'} – user1050619

+0

你应仔细检查您在请求中发送的数据。我试着将你在这里展示的内容复制并粘贴到一个Python shell中,并且它缺少一个尾部的'}'。 –

+0

我已更新我的答案,以包含正确编码您的请求的信息 –