在django休息中获取JSON的错误结果

时间:2016-06-10 11:50:01

标签: python json django django-rest-framework

尝试发送此JSON:

{"delivery":{"delivery_time":"2016-05-31T12:18:47Z","delivery_adress":"123","phone":"123"},"paymentMethod":{"id":"1","title":"123124123"},"summ":"23","success":false}

到我的序列化器:

class OrderSerializer(serializers.ModelSerializer):

    delivery = DeliverySerializer(required=False, allow_null=True)
    paymentMethod = PaymentsSerializer(required=False, allow_null=True)
    class Meta:
        model = Order
        fields = ( 'delivery', 'paymentMethod','summ','success') #'id',

    def create(self, validated_data):
        deliverys_data = validated_data.pop('delivery')
        delivery = Delivery.objects.create(**deliverys_data)
        paymentsMethod_data = validated_data.pop('paymentMethod')
        data=list(paymentsMethod_data.values())[:3]
        print(data)
        payment_method = Payments.objects.create(**paymentsMethod_data)
        order = Order.objects.create(delivery=delivery,paymentMethod=payment_method,**validated_data)
        return order

我需要获取id并将其用作主键,但是打印结果:[u' 123124123'],

而且我不知道JSON丢失了我的身份。

查看

@api_view(['POST'])
def order_post(request, format=None):
    if request.method == 'POST':
        serializer = OrderSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

其他序列化程序

class DeliverySerializer(serializers.ModelSerializer):
    class Meta:
        model = Delivery
        fields = ('delivery_time','delivery_adress','phone')#'id', 
    def create(self, validated_data):
        return Delivery.objects.create(**validated_data)   


class PaymentsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Payments
        fields = ('id','title') #
    def create(self, validated_data):
        return Payments.objects.create(**validated_data)

但是在转到PaymentSerializer之前我必须得到id。

0 个答案:

没有答案