I have an array of objects:
[
{
order: 4,
id: 1,
},
{
order: 2,
id: 2,
}
]
Where id is the primary key and order is the value to update, I'm posting to the server 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 I filter by the id and update its respective order in a for. But it is throwing me the following error:
AttributeError: 'list' object has no attribute 'items'
Am I missing something? since when I send only one this.ordsend[0]
and I remove the for if it comes to update the element.
OK after many attempts and research I solved it this way: First from axios I sent it from an object:
and on the server change the for to :
That is, enter the name of the object and it worked :) I hope it helps someone.