“L'instance de requête est immuable” Réponses codées

AttributError: cette instance de requête est immuable 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'] = "[email protected]"
        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')
codeAdept

L'instance de requête est immuable

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'}>
Markhor

Réponses similaires à “L'instance de requête est immuable”

Questions similaires à “L'instance de requête est immuable”

Plus de réponses similaires à “L'instance de requête est immuable” dans Python

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code