How to update multiple instances in a single post - Django rest framework

0

I have an array of objects:

[
    {
      order: 4,
      id: 1,
    },
    {
      order: 2,
      id: 2,
    }
]

Where id is the primary key and the order is the value to update, I am sending to the server by post with axios:

this.axios.post('route-dispatchs/order/', this.ordsend)
   .then((response) => {
     console.log('success');
});

And my Viewset :

@list_route(methods=['post'], url_path='order')
    def order_dispatch(self, request):
        for data in request.data:
            self.queryset.filter(id=data['id']).update(order=data['order'])
        page = self.paginate_queryset(self.queryset)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)
        serializer = self.get_serializer(self.queryset, many=True)
        return Response(serializer.data)

In which filter by the id and update its respective order in a for. But he is throwing me the following error: AttributeError: 'list' object has no attribute 'items'

Is something missing? since when I send only one this.ordsend[0] and I remove the for if it arrives to update the element.

Error image:

    
asked by Piero Pajares 06.04.2018 в 15:18
source

1 answer

0

OK after many attempts and research solve it this way: First from axios I sent it from an object:

this.axios.post('route-dispatchs/order/', {'sort': this.ordsend})

and on the server change the for for:

for data in request.data['sort']:

That is, enter the name of the object and it works :) I hope it serves someone.

    
answered by 08.04.2018 / 14:32
source