Changing serializer fields on the fly

后端 未结 4 1490
别跟我提以往
别跟我提以往 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:30

    You appear to be looking for a write-only field. So the field will be required on creation, but it won't be displayed back to the user at all (the opposite of a read-only field). Luckily, Django REST Framework now supports write-only fields with the write_only attribute.

    In Django REST Framework 3.0, you just need to add the extra argument to the extra_kwargs meta option.

    class UserSerializer(serializers.ModelSerializer):
    
        class Meta:
            model = User
            fields = (
                'userid',
                'password'
            )
            extra_kwargs = {
                'password': {
                    'write_only': True,
                },
            }
    

    Because the password should be hashed (you are using Django's user, right?), you are going to need to also hash the password as it is coming in. This should be done on your view, most likely by overriding the perform_create and perform_update methods.

    from django.contrib.auth.hashers import make_password
    
    class UserViewSet(viewsets.ViewSet):
    
        def perform_create(self, serializer):
            password = make_password(self.request.data['password'])
    
            serializer.save(password=password)
    
        def perform_update(self, serializer):
            password = make_password(self.request.data['password'])
    
            serializer.save(password=password)
    

    In Django REST Framework 2.x, you need to completely redefine the password field on the serializer.

    class UserSerializer(serializers.ModelSerializer):
        password = serializers.CharField(write_only=True)
    
        class Meta:
            model = User
            fields = (
                'userid',
                'password'
            )
    

    In order to hash the password ahead of time in Django REST Framework 2.x, you need to override pre_save.

    from django.contrib.auth.hashers import make_password
    
    class UserViewSet(viewsets.ViewSet):
    
        def pre_save(self, obj, created=False):
            obj.password = make_password(obj.password)
    
            super(UserViewSet, self).pre_save(obj, created=created)
    

    This will solve the common issue with the other answers, which is that the same serializer that is used for creating/updating the user will also be used to return the updated user object as the response. This means that the password will still be returned in the response, even though you only wanted it to be write-only. The additional problem with this is that the password may or may not be hashed in the response, which is something you really don't want to do.

提交回复
热议问题