Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a PUT (partial update) using generics in Django-Rest-Framework?

If I have a class view that looks like this,

class MovieDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Movie.objects.all()
    serializer_class = MovieSerializer

how do I make the serialize accept partial updates? currently where it stands Put will erase an existing data for said object.

like image 847
Chris Hawkes Avatar asked Apr 21 '15 01:04

Chris Hawkes


Video Answer


1 Answers

If you are using the DRF route, use PATCH method instead of PUT.

if you write the urls configuration by yourself, dispatch it to partial_update method in your RetrieveUpdateDestroyAPIView view.

If you get the serialize by yourself, pass the partial=True to your Serializer

partial = kwargs.pop('partial', False)
serializer = self.get_serializer(instance, data=request.data, partial=partial)
like image 143
soooooot Avatar answered Oct 13 '22 02:10

soooooot