Changing serializer fields on the fly

后端 未结 4 1501
别跟我提以往
别跟我提以往 2020-12-11 03:14

For example I have the following serializer:

class UserSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = (
             


        
4条回答
  •  被撕碎了的回忆
    2020-12-11 03:40

    Just one more thing to @Kevin Brown's solution.

    Since partial update will also execute perform_update, it would be better to add extra code as following.

    def perform_update(self, serializer):                                                           
        if 'password' in self.request.data:
            password = make_password(self.request.data['password'])                                 
            serializer.save(password=password)                                                      
        else:
            serializer.save()
    

提交回复
热议问题