How can modify request data in django REST framework

0 votes

I am using Django REST Framework

request.data = '{"id": "10", "user": "Niroj"}'

I want to add extra attribute like "age": "30" before sending it to further like

    request.data = new_data
    response = super().post(request, *args, **kwargs)

I have two issues

  1. Why request.data is coming as string rather than dict
  2. How can i update the request.data
Jul 1, 2020 in Python by kartik
• 37,490 points
1,423 views

1 answer to this question.

0 votes

Hello @kartik,

Generally request in drf views is rest_framework.request.Request instance. Following to it's source code (djangorestframework==3.8.2):

    @property
    def data(self):
        if not _hasattr(self, '_full_data'):
            self._load_data_and_files()
        return self._full_data

You can do:

request._full_data = your_data

Hope it helps!!
Thank You!

answered Jul 1, 2020 by Niroj
• 82,580 points

Related Questions In Python

0 votes
1 answer

How to return custom JSON in Django REST Framework?

Hello @kartik, There are 2 ways to custom ...READ MORE

answered Jun 25, 2020 in Python by Niroj
• 82,580 points
3,947 views
0 votes
1 answer

How to secure APIs for Registration and Login in Django Rest Framework?

Hello @kartik, you cannot have an authentication system ...READ MORE

answered Jun 25, 2020 in Python by Niroj
• 82,580 points
1,518 views
0 votes
1 answer

How to disable a method in a ViewSet, django-rest-framework

Hello @kartik, You could keep using viewsets.ModelViewSet and define http_method_names on your ...READ MORE

answered Jun 25, 2020 in Python by Niroj
• 82,580 points
2,654 views
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,580 points
618 views
0 votes
1 answer

How do I use Django templates without the rest of Django?

Hello @kartik, Let's say you have this important ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,580 points
256 views
0 votes
1 answer

How to add annotate data in django-rest-framework queryset responses?

Hello @kartik, The queryset returned from get_queryset provides ...READ MORE

answered Jun 25, 2020 in Python by Niroj
• 82,580 points
1,480 views
0 votes
1 answer

How to get Request.User in Django-Rest-Framework serializer?

Hello @kartik, You cannot access the request.user directly. You need ...READ MORE

answered Aug 12, 2020 in Python by Niroj
• 82,580 points
2,563 views