Home > other >  dict object has no attribute
dict object has no attribute

Time:02-12

In frontend I use vue js , using axios I sent data to backend.But after clicking submit button I got this error: dict object has no attribute 'invoice_products'.

From frontend using axios:

        this.$http.post('http://127.0.0.1:8000/api/createTest', {
            invoice_products: this.invoice_products
        })

This is my json input data

{"invoice_products":[{"name":"fgf","price":"56"}]}

views.py:

@api_view(['POST'])
def createTest(request):
    serializer = TestSerializer(data=request.data.invoice_products)
    if serializer.is_valid():
        serializer.save()
    return Response(serializer.data)

Error: dict object has no attribute 'invoice_products'

CodePudding user response:

request.data is a dict. So you access its info with request.data[yourkey] not request.data.yourkey


@api_view(['POST'])
def createTest(request):
    serializer = TestSerializer(data=request.data['invoice_products'])
    if serializer.is_valid():
        serializer.save()
    return Response(serializer.data)

Depending of what you serializer looks like, you may need to add many=true option to TestSerializer

  • Related