djangodjango-rest-frameworkfieldserializationwriteonly

django rest framework write only field usage


I just want to know usage of write_only that is option password1,2 fields below

I checked view returns fileds information without password, so i could understand roughly but cannot find what exactly write_onlyoptions usage is.

please somebody explain or leave reference document link..

class SignupSerializer(serializers.ModelSerializer):
    password1 = serializers.CharField(write_only=True)
    password2 = serializers.CharField(write_only=True)
    token = serializers.SerializerMethodField()

    class Meta:
        model = User
        fields = (
            'username',
            'password1',
            'password2',
        )

this is view

class Signup(APIView):
    def post(self, request):
        serializer = SignupSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, 
    status=status.HTTP_400_BAD_REQUEST)

Solution

  • write_only is exactly what it says.

    When you use the enclosing serializer class in an APIView of ViewSet for 'GET' methods, DRF won't include the field when serializing the representation.

    Here is the api documentation for write_only