Inconsistent validation for implicit ManyRelatedField on ModelSerializer #7753
Replies: 6 comments
-
Duplicating my response on the PR: |
Beta Was this translation helpful? Give feedback.
-
My understanding is that that the In fact, if I declare the field explicitly with
Also, if you add |
Beta Was this translation helpful? Give feedback.
-
Reopening for further investigations |
Beta Was this translation helpful? Give feedback.
-
On closer inspection, I think I was looking at this kind of backward. As you mentioned, @xordoquy , the html forms in the browsable API don't support setting a list field to null or omitting it; if no items in the multi select widget are selected, the field is deserialized as an empty list. To be omitted, I guess the field would need to be dynamically removed from the DOM with javascript. So I guess the real issue is, why is an empty list valid when setting |
Beta Was this translation helpful? Give feedback.
-
That's the reason why I reopened the issue ;) |
Beta Was this translation helpful? Give feedback.
-
Any solution? |
Beta Was this translation helpful? Give feedback.
-
failing test: #4936
When using a ModelSerializer for a model with a ManyToMany field, and applying
required=False
to that field inextra_kwargs
, omitting the ManyToManyField in the data results in a validation error ("This list may not be empty.") for form data requests (including from the browsable api form), but not for JSON requests.On closer inspection, I found that when using the serializer directly, passing a
dict
, the validation error doesn't appear; whereas when passing aQueryDict
it does.Checklist
master
branch of Django REST framework.Steps to reproduce
I created a toy project to reproduce the bug.
Expected behavior
The validation shouldn't differ between form data requests and JSON requests. Because the field has the option
required=False
, omitting the field shouldn't prevent deserialization; thus the JSON behaviour is correct.Actual behavior
When passing the data in a dict, omitting the many to many field ('tags' in the toy project linked above), the data passes validation; whereas passing a QueryDict (or making a request with form data rather than json) results in a validation error with message "This list may not be empty."
Beta Was this translation helpful? Give feedback.
All reactions