Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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')
Comment

querydict instance is immutable

You can convert it to a mutable QueryDict instance by copying it:

request.GET = request.GET.copy()
Afterwards you'll be able to modify the QueryDict:

>>> from django.test.client import RequestFactory
>>> request = RequestFactory().get('/')
>>> request.GET
<QueryDict: {}>
>>> request.GET['foo'] = 'bar'
AttributeError: This QueryDict instance is immutable
>>> request.GET = request.GET.copy()
<QueryDict: {}>
>>> request.GET['foo'] = 'bar'
>>> request.GET
<QueryDict: {'foo': 'bar'}>
Comment

PREVIOUS NEXT
Code Example
Python :: progress bar python text 
Python :: print for loop in same line python 
Python :: absolute value in python 
Python :: np.where 
Python :: read dict from text 
Python :: how to make button in python 
Python :: semicolon in python 
Python :: read and write to file python 
Python :: how to get the parent class using super python 
Python :: python get line number x in file 
Python :: letters to numbers python 
Python :: pandas resample groupby 
Python :: django group with permission 
Python :: messagebox python pyqt 
Python :: python area of rectangle 
Python :: python train test val split 
Python :: find max length of list of list python 
Python :: select columns pandas 
Python :: subtract list from list python 
Python :: python open google 
Python :: pandas dataframe get first n rows 
Python :: python raise exception 
Python :: how to write to a specific line in a file python 
Python :: python plot horizontal line 
Python :: flask flash 
Python :: RGB To Hex Conversion python 
Python :: reversed function python 
Python :: precision and recall from confusion matrix python 
Python :: driver find element with multiple classes python 
Python :: not equal python 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =