Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django Rest Framework.DATA属性仅返回数组的第一项_Python_Django_Django Rest Framework - Fatal编程技术网

Python Django Rest Framework.DATA属性仅返回数组的第一项

Python Django Rest Framework.DATA属性仅返回数组的第一项,python,django,django-rest-framework,Python,Django,Django Rest Framework,我有一个API端点,客户端在其中发布一个JSON对象(一次邀请几个用户加入一个项目) 我的测试如下所示: def test_new_style(self): note = 'this is a note' payload = { 'invites': [ { 'email': 'test2@getmixim.com', 'note': note },

我有一个API端点,客户端在其中发布一个JSON对象(一次邀请几个用户加入一个项目)

我的测试如下所示:

def test_new_style(self):
    note = 'this is a note'
    payload = {
        'invites': [
            {
                'email': 'test2@getmixim.com',
                'note': note
            },
            {
                'email': 'notauser@getmixim.com',
                'note': note
            }
        ]
    }

    # self.u1_client is a rest_framework.test.APIClient object
    response = self.u1_client.post('/api/projects/1/invite', payload)
我有一个APIView,看起来像:

class InviteMember(APIView):
    permission_classes = (IsAuthenticated,)

    def post(self, request, project_pk):
        import pdb; pdb.set_trace()
我在壳中着陆,并执行以下操作:

(Pdb) request
<rest_framework.request.Request object at 0x106bb4910>
(Pdb) request.DATA
<QueryDict: {u'invites': [u"{'note': 'this is a note', 'email': 'test2@getmixim.com'}", u"{'note': 'this is a note', 'email': 'notauser@getmixim.com'}"]}>
(Pdb) request.DATA['invites']
u"{'note': 'this is a note', 'email': 'notauser@getmixim.com'}"
(Pdb)请求
(Pdb)request.DATA
(Pdb)请求数据['invests']
u“{'note':'这是一张便条','电子邮件':'notauser@getmixim.com'}"
奇怪吧?如何获取invite字典数组?为什么数据属性不只是给我对象


Django:v1.7.4

Django Rest框架:v2.4.4

我找到了解决方案!问题是我的请求被作为查询字符串发送

将我的测试更改为

response = self.u1_client.post('/api/projects/1/invite', payload, format='json')
…解决了问题


实际上,DRF APIClient将各个dict编码为JSON,然后将它们嵌入查询字符串中。

的确如此。在master now中,如果嵌套结构在测试用例中被传递到多部分编码器,我们将发出一条有用的错误消息。@TomChristie这非常有用,谢谢。另外,谢谢你的DRF,使用这样一个设计良好的框架是一件很高兴的事。你救了我一天