关于python:在django restframework中序列化数据

Serialize data in django restframework

我通过django restframework中的post请求获得以下数据。我需要序列化此数据,并且此数据包含多个模型的数据。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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视图-

1
2
3
4
5
6
7
8
9
10
11
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


不需要手动解码视图中的日志数据。只要您使用JSONParser,如解析器文档中所述,request.data就应该已经被完全解析了。

此外,看起来您发送到视图的请求可能并没有按照您的想法执行。如果您想发送带有请求的JSON数据,您需要更明确一点。如请求文档中的示例所示:

1
2
3
4
5
>>> import json
>>> url = 'https://api.github.com/some/endpoint'
>>> payload = {'some': 'data'}

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