gpt4 book ai didi

python - DRF 测试客户端无法发布 JSON 列表

转载 作者:行者123 更新时间:2023-11-28 19:43:24 25 4
gpt4 key购买 nike

我创建了一个 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 列表的方式有问题吗?

最佳答案

您应该将 json 指定为格式:

response = self.client.post(cart_item_url, data=data, format='json')

关于python - DRF 测试客户端无法发布 JSON 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31271754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com