AttributeError: This QueryDict instance is immutable django 1

AttributeError: This QueryDict instance is immutable django

#views.py
from rest_framework import generics


class Login(generics.CreateAPIView):
    serializer_class = MySerializerClass
    def create(self, request, *args, **kwargs):
        request.data._mutable = True
        request.data['username'] = "example@mail.com"
        request.data._mutable = False

#serializes.py
from rest_framework import serializers


class MySerializerClass(serializers.Serializer):
    username = serializers.CharField(required=False)
    password = serializers.CharField(required=False)
    class Meta:
        fields = ('username', 'password')

Here is what the above code is Doing:
1. The create method is being overridden.
2. The request.data is being made mutable.
3. The username is being set to a default value.
4. The request.data is being made immutable again.
5. The serializer is being called.

Similar Posts