-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Description
This took me quite a while to work out :)
I have a Serializer with HyperlinkedRelatedField(many=True) on it. I had some API tests that would pass in an empty dict:
self.client.post(list_url, {}, format='json')
The response yielded the expected 401, stating that the given field was required. So far, so good.
Much to my surprise, if I sent an empty POST request to the same url on my production system (or Django's built-in development server), the resource was created succesfully, with an empty list for that field.
After quite a bit of digging, I learned that an empty request invariably means that request.data is a QueryDict, rather than a simple, empty dict. During validation, many Field implementations call html.is_html_dictionary() on request.data. is_html_dictionary simply checks for a .getlist() method. Incidentally, this getlist() method is the culprit. When called, it will always return a list, even if the given key is not found at all, in which case it yields an empty list.
I feel that the result for an empty request and a request that passes in an empty json object should yield the exact same results.