93π
As you can see in the Django documentation:
The QueryDicts at request.POST and request.GET will be immutable when accessed in a normal request/response cycle.
so you can use the recommendation from the same documentation:
To get a mutable version you need to use QueryDict.copy()
or β¦ use a little trick, for example, if you need to keep a reference to an object for some reason or leave the object the same:
# remember old state
_mutable = data._mutable
# set to mutable
data._mutable = True
# Ρhange the values you want
data['param_name'] = 'new value'
# set mutable flag back
data._mutable = _mutable
where data it is your QueryDicts
22π
Do Simple:
#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')
- [Django]-LEFT JOIN Django ORM
- [Django]-Django filter vs exclude
- [Django]-What is the right way to validate if an object exists in a django view without returning 404?
9π
request.data._mutable=True
Make mutable true to enable editing in querydict or the request.
- [Django]-Create if doesn't exist
- [Django]-Do properties work on Django model fields?
- [Django]-Celery β Get task id for current task
4π
You can use request=request.copy()
at the first line of your function.
- [Django]-Running ./manage.py migrate during Heroku deployment
- [Django]-Django-social-auth django-registration and django-profiles β together
- [Django]-Django β Rotating File Handler stuck when file is equal to maxBytes
3π
I personally think it would be more elegant to write code like this.
def create(self, request, *args, **kwargs):
data = OrderedDict()
data.update(request.data)
data['account'] = request.user.account
serializer = self.get_serializer(data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
- [Django]-What is "load url from future" in Django
- [Django]-How to mix queryset results?
- [Django]-Setting the selected value on a Django forms.ChoiceField
2π
Do you know any better way to add additional fields when creating an object with django rest framework?
The official way to provide extra data when creating/updating an object is to pass them to the serializer.save()
as shown here
- [Django]-Django Rest Framework Token Authentication
- [Django]-Django: Generic detail view must be called with either an object pk or a slug
- [Django]-Using window functions in an update statement
1π
https://docs.djangoproject.com/en/2.0/ref/request-response/#querydict-objects
The QueryDicts at request.POST and request.GET will be immutable when accessed in a normal request/response cycle. To get a mutable version you need to use QueryDict.copy().
- [Django]-Django signals vs. overriding save method
- [Django]-How to use UUID
- [Django]-How does Django's nested Meta class work?