Reputation: 445
How can I use 'serializer.initial_data' in Django Rest-API. What is the difference between using request.data and serializer.initial_data.
Upvotes: 3
Views: 7521
Reputation: 1424
Requests.data contains the data that comes in from the frontend. Arguably, you should leave requests.data
alone and use a serializer to convert said data into more complex types. The use cases for serializers are pretty vast ranging, but the most common is translation of data into database model instances.
Upvotes: 3
Reputation: 88619
When passing data to a serializer instance, the unmodified data will be made available as .initial_data
. If the data
keyword argument is not passed then the .initial_data
attribute will not exist.
serializer = MyFooSerializer(data={'foo':'bar'})
print(serializer.initial_data) # this will print {'foo':'bar'}
The request.data
returns the parsed content of the request body.
Upvotes: 7