我创建了一个ListCreateAPIView
,如下所示:
13 class CartItemView(generics.ListCreateAPIView):
14 serializer_class = CartItemSerializer
15
16 def get_serializer(self, *args, **kwargs):
17 if 'data' in kwargs:
18 data = kwargs['data']
19 if isinstance(data, list):
20 kwargs['many'] = True
21
22 return super(CartItemView, self).get_serializer(*args, **kwargs)
23
这是串行器:
47 class CartItemSerializer(serializers.ModelSerializer):
48 menu_item = serializers.PrimaryKeyRelatedField(queryset = MenuItem.objects.all())
49 cart = serializers.PrimaryKeyRelatedField(queryset = Cart.objects.all(), required=False)
50
51 class Meta:
52 model = CartItem
53 depth = 1
54
55 def create(self, validated_data):
56 menu_item = validated_data['menu_item']
57 cart = validated_data['cart']
58 cart_item = CartItem.objects.create(menu_item = menu_item, cart = cart, no_of_units = validated_data['no_of_units'])
59 if cart.cart_state == Cart.EMPTY:
60 cart.item_added_to_cart()
61 cart.save()
62 return cart_item
现在,我正试图从测试中发布一个JSON对象列表,如下所示:
1 from rest_framework.test import APITestCase
.....
17 class CartItemViewTest(APITestCase):
18 def test_create_cart_items(self):
22 data = [{'cart': '4', 'total_qty': '5', 'menu_item': '1'}]
23 cart_item_url = '/api/v1/cart/items/'
24 response = self.client.post(cart_item_url, data=data)
26 print response.data
29 self.assertEqual(len(resp.data['cart_items']), 1)
但它会引发以下错误:
======================================================================
ERROR: test_create_cart_items (app.tests.CartItemViewTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/ubuntu/src/app/tests.py", line 24, in test_create_cart_items
response = self.client.post(cart_item_url, data=data)
File "/home/ubuntu/Envs/rj-venv/local/lib/python2.7/site-packages/rest_framework/test.py", line 168, in post
path, data=data, format=format, content_type=content_type, **extra)
File "/home/ubuntu/Envs/rj-venv/local/lib/python2.7/site-packages/rest_framework/test.py", line 89, in post
data, content_type = self._encode_data(data, format, content_type)
File "/home/ubuntu/Envs/rj-venv/local/lib/python2.7/site-packages/rest_framework/test.py", line 64, in _encode_data
ret = renderer.render(data)
File "/home/ubuntu/Envs/rj-venv/local/lib/python2.7/site-packages/rest_framework/renderers.py", line 678, in render
return encode_multipart(self.BOUNDARY, data)
File "/home/ubuntu/Envs/rj-venv/local/lib/python2.7/site-packages/django/test/client.py", line 168, in encode_multipart
for (key, value) in data.items():
AttributeError: 'list' object has no attribute 'items'
顺便说一句,当我从REST客户端启动该API时,它运行得非常好。我使用测试客户端发布json列表的方式有什么问题吗?
答案 0 :(得分:9)
您应该将json指定为格式:
response = self.client.post(cart_item_url, data=data, format='json')
答案 1 :(得分:1)
尝试将application/json
指定为response = self.client.post(cart_item_url, data=json.dumps(data), content_type='application/json')
。
{{1}}
答案 2 :(得分:0)
尝试:
response = self.client.post(cart_item_url, data=json.dumps(data)), headers={'Content-Type': 'application/json'})
答案 3 :(得分:0)
你可以试试这个
response = self.client.post(cart_item_url, json.dumps(data), content_type='application/json')
这对我有用