Immutable QueryDict in Serializer on PATCH and PUT requests

827 views
Skip to first unread message

leager...@rapidsos.com

unread,
Jul 18, 2016, 6:35:10 PM7/18/16
to Django REST framework
Hello, 

We are using a (admittedly somewhat hacky) solution to modify data coming into the serializer before we store it in the database. We do this by overriding the to_internal_value method of the ModelSerializer class to extract certain fields from the data dictionary, modifying them, and then placing the modified versions back into the data dictionary and then calling the super class (ModelSerializer) to_internal_value method with the modified data dictionary. When making calls from our client applications and from applications like postman and the DRF browsable api, this works just fine. But when we do this from an automated testing tool that we wrote, it fails on PATCH and PUT requests saying the data dictionary is an immutable QueryDict. We have tried setting different Content-type headers, using PATCH and PUT requests, and different data formats but nothing works. Again, this request works just fine from the other clients mentioned. Could you give some guidance as to when and why the data dictionary in the serializer may be immutable?

Thank you

Kevin Brown

unread,
Jul 18, 2016, 6:39:41 PM7/18/16
to Django REST framework

If the data modifications do not depend on other fields (so the changes are applied to a single field based only on that field), you may just want to do the changes in the 'validate_fieldname' method. You would just return the modified data from it, and it will work with any incoming data type. This only works if the original data can pass the initial validation though.

Note that the reason why you are seeing this issue in tests and not when using postman is because tests send the input as form data, while postman is likely sending it as JSON. Form data is converted into an immutable QueryDict, while JSON data is a standard mutable dictionary.


--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

leager...@rapidsos.com

unread,
Jul 20, 2016, 11:36:56 AM7/20/16
to Django REST framework
Hi Kevin,

Thanks for the idea to use the validate_fieldname method. However, with the test tool we use that I have mentioned, we actually do submit the data as JSON and even went as far as manually setting the Content-Type header to application/json to ensure it was being recognized as such. Still no luck. It claimed it was an immutable dictionary leading to a lot of confusion since I was under the same understanding you are. Any thoughts on that?
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages