pythondjangopython-3.xdjango-rest-framework

What does 'serializer.initial_data' mean?


How can I use serializer.initial_data in Django Rest-API? What is the difference between using request.data and serializer.initial_data?


Solution

  • 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.