django serializer小结(三)

复写update方法,只更新部分字段。

def update(self, request, *args, **kwargs):
    instance = self.get_object()
    print(instance,instance.id)
    data=request.data.copy()
    data["number"] = instance.number
    data["workshop"] = ','.join([self.dict_roles[roles] for roles in data['roles_id']])
    ser = self.serializer_class(data=data, instance=instance)
    if ser.is_valid(raise_exception=True):
        ser.save()
        return Response({'info': 'ok'})
    else:
        return Response({'info': 'error'})

 

你可能感兴趣的:(Django)