HTTP PUT not working properly in django rest framework -


i using following code in django rest framework achieve put , patch functionality. patch (partial_update) works fine, however, providing fewer fields put not replace whole resource put request should. instance, if data seems following:

{ "id": 6, "name": "crticial mvp", "description": "getting things done", "end": "2012-02-18"    } 

and if attempt put following data(on http://localhost:800/api/sprints/6/) :

{     "name": "critical mvp2",      "end": "2012-02-18" } 

this call should set description none. description retains value if doing patch. how enforce resource replacement in update functionality?

ps: have set partial=false when initializing serializer update.

views.py:

class sprintviewset(viewsets.modelviewset):     queryset = sprint.objects.order_by('end')     serializer_class = sprintserializer      def update(self, request, *args, **kwargs):         sprint=self.get_object()                                 serializer = sprintserializer(sprint, data=request.data, partial=false,  context={'request': request, 'view':self, 'format':none})         serializer.is_valid(raise_exception=true)                serializer.save()         return response(serializer.data)     def partial_update(self, request, *args, **kwargs):         instance = self.get_object()         serializer = sprintserializer(instance, data=request.data, partial=true,  context={'request': request, 'view':self, 'format':none})         serializer.is_valid(raise_exception=true)         new_instance = serializer.save()         return response(serializer.data) 

serializers.py:

class sprintserializer(serializers.modelserializer):     links = serializers.serializermethodfield()     class meta:              model = sprint                       fields = ('id', 'name', 'description', 'end', 'links')     def get_links(self, obj):         request = self.context['request']          return {                 'self': reverse('sprint-detail',                 kwargs={'pk': obj.pk},request=request),                 } 

models.py

class sprint(models.model):      name = models.charfield(max_length=100, blank=true, default='')     description = models.textfield(blank=true, default='')     end = models.datefield(unique=true)     def __str__(self):         return self.name or _('sprint ending %s') % self.end 

a put expects full data object passed in, attributes on model replaced data passed in. requires full data object passed in, if don't pass in field ignored depending on how data sent.

if sending data in using form data (ajax without json, or html <form>), field ignored. has browsers not sending fields (such checkboxes) if value isn't set, drf manually overrides in these situations.

if using json , field not required (which appears case), drf override value if key passed request. because put supposed contain full data object, , drf makes few assumptions based on this.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -