如何将字符串序列化为列表?

时间:2019-04-04 10:54:26

标签: python django django-rest-framework

但是当我发布这样的产品时:

...
function (response) {
    var arr = response.riskPoint;
    for(var i = 0;i<arr.length;i++){
        // read arr[i];
    }

}

我期望有一本字典,但有清单

"product": [
            {
                "product_id": 1,
                "product_name": "product",
                "quantity": 1
            },
             {
                "product_id": 2,
                "product_name": "product1",
                "quantity": 3
            }

        ]

serializers.py

Full error:

Internal Server Error: /api/ordering/
Traceback (most recent call last):
  File "/home/neha/.local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/neha/.local/lib/python3.5/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/neha/.local/lib/python3.5/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/neha/.local/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/neha/.local/lib/python3.5/site-packages/rest_framework/viewsets.py", line 116, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/neha/.local/lib/python3.5/site-packages/rest_framework/views.py", line 495, in dispatch
    response = self.handle_exception(exc)
  File "/home/neha/.local/lib/python3.5/site-packages/rest_framework/views.py", line 455, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/neha/.local/lib/python3.5/site-packages/rest_framework/views.py", line 492, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/neha/.local/lib/python3.5/site-packages/rest_framework/mixins.py", line 21, in create
    self.perform_create(serializer)
  File "/home/neha/.local/lib/python3.5/site-packages/rest_framework/mixins.py", line 26, in perform_create
    serializer.save()
  File "/home/neha/.local/lib/python3.5/site-packages/rest_framework/serializers.py", line 214, in save
    self.instance = self.create(validated_data)
  File "/home/neha/Documents/Order/ordering/serializers.py", line 154, in create
    output_dict = json.loads((input_dict))
  File "/usr/lib/python3.5/json/__init__.py", line 312, in loads
    s.__class__.__name__))
TypeError: the JSON object must be str, not 'list'

viewsets.py

class CustOrderSerializer(serializers.ModelSerializer):

    price = serializers.SlugRelatedField(slug_field='price', queryset=Price.objects.all())
    # product = serializers.SlugRelatedField(slug_field='product', queryset=Product.objects.all())
    area = serializers.SlugRelatedField(slug_field='address', queryset=Area.objects.all())
    city = serializers.SlugRelatedField(slug_field='city', queryset=City.objects.all())
    coupon = serializers.SlugRelatedField(slug_field='code', queryset=Coupon.objects.all())
    # coupon = CouponSerializer()
    product = ProductSerializer(many=True)
    class Meta:
        model = CustOrder
        fields = '__all__' 
 def create(self, validated_data):
        product = validated_data.pop('product')
        customerorder = CustOrder.objects.create(**validated_data)
        print (type (customerorder))
        print(customerorder)
        print(product)
        print("*************************************")
        input_dict = product
        output_dict = json.loads(json.dumps(input_dict))
        print (output_dict)
        print("*************************************")
         input_dict = product

        output_dict = json.loads((input_dict))
        # print (output_dict)
        customerorder.product.add(
         )

        Product.objects.create(product= [ProductItems.product], )
        return customerorder

我的 viewsets.py

class CustOrderViewSet(viewsets.ModelViewSet):
    queryset = CustOrder.objects.all()
    permission_classes = [
        permissions.AllowAny
    ]
    serializer_class = CustOrderSerializer

0 个答案:

没有答案