Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7k
Fix parsing multipart data using a nested serializer with list#3820
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
TestNestedSerializerWithList checks if a list (a MulitpleChoiceField inthis case) in a nested serializer is correctly parsed from json ormultipart formatted data.
It is possible that a key in a MultiValueDict has multiple values, listsare represented this way. When accessing a key in a MultiValueDictit only returns the last element of that key. This becomes a problemwhen parsing an html dict with a list inside of it.To fix this problem we have to get and set the value using .getlist()and .setlist().This commit solves a bug that caused a list in a nested serializer tonot being properly parsed if it was sent as multipart formated data.
Contributor
lovelydinosaur commentedJun 23, 2016
Yup, looks good. Thanks! |
This was referencedMar 9, 2017
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading.Please reload this page.
Parsing multipart data using a nested serializer with a list can result in incorrect behavior. If the list contains more than one item, only the last item will be saved.
Here is an example:
When you send the following data formatted as JSON everything will be fine:
{'nested': {'example': [1,2], }}# --->{'nested': {'example': [1,2], }}However, when you send the same data formatted as multipart data the serialized object will only take the last element of the list:
{'nested.example': [1,2]}# --->{'nested' {'example':2, }}The bug is situated in
parse_html_dict(dictionary, prefix='')inrest_framework.utils.html. This function takes and returns a newMultiValueDict. A key in aMultiValueDictcan have multiple values, this is how a list is represented. When accessing a key in aMultiValueDictonly the last element of the list is returned.This can be easily solved by using .getlist() and .setlist() when getting and setting the value of the
MultiValueDict.Closes#3710.