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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clearing a ManyToManyField in a multipart PATCH Request #2883
Comments
I think the crux of this boils down to the fact that there's no way to encode from rest_framework.test import APIClient
client = APIClient()
client._encode_data({'foo':[1, 2, 3]}, format='multipart')
('--BoUnDaRyStRiNg\r\nContent-Disposition: form-data; name="foo"\r\n\r\n1\r\n--BoUnDaRyStRiNg\r\nContent-Disposition: form-data; name="foo"\r\n\r\n2\r\n--BoUnDaRyStRiNg--\r\n', u'multipart/form-data; boundary=BoUnDaRyStRiNg; charset=utf-8')
client._encode_data({'foo': []}, format='multipart')
('--BoUnDaRyStRiNg--\r\n', u'multipart/form-data; boundary=BoUnDaRyStRiNg; charset=utf-8') |
Correct. This is a constraint of HTML form input. We can either document this, or instead simply not display (or disable with a tooltip) the PATCH button for form input, and only enable it for raw JSON input. See also #2894 |
My workaround is listed below. Hope we will have a nice solution later. Using null when firing an ajax request.
Add some code to clear ManyToManyField in is_valid method
|
This comment from 2017 just saved my sanity. |
I am open to review an implementation which can implement it properly without any side effect |
I have totally no idea for what I was doing at that moment. :) It's glad to see the snippet can help you a little bit. |
Hello,
I'm having some trouble clearing a
ManyToMany
field in a multipart PATCH request.In a JSON request, I would clear my
ManyToMany
field,'many_to_many_field'
by PATCHing something like:{"many_to_many_field": []}
However, in a multipart PATCH request, sending values including
'[]'
,''
,'null'
,'None'
, etc all yield the following error:["Incorrect type. Expected pk value, received unicode."]
It seems that
ManyRelatedField.get_value()
does not recognize any of those options as special indicators ofempty
.The text was updated successfully, but these errors were encountered: