Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

dict object has no attribute

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{"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’

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading