-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Fix support for ListField default values in HTML mode #5812
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
Fix support for ListField default values in HTML mode #5812
Conversation
The ListField handles html forms data differently than json data. This section would always return a value (an empty list) for a list field, even when that field name was not posted at all. This change checks for the field being present and returns `empty` if it is not; this lets the standard default-value and required field handling take over.
I need to put more thought into this, but I don't think this is correct. HTML forms always submit their inputs (no way to not submit an input), so form processing doesn't usually differentiate between an empty value and the lack of that input's presence. e.g., django-rest-framework/rest_framework/fields.py Lines 1612 to 1616 in c456b3c
I'm thinking it might be more appropriate to return an |
Currently Also, its not just browsers that submit forms as multipart/form-data. In my case, we are submitting
Admittedly this is a bit of a sticky issue, and I can't see all the angles. |
Updated slightly to allow for posting fields in the form The previous change ignored this, causing indexed fields to be ignored since the exact field name wasn't in the dictionary ( |
@@ -1609,6 +1609,9 @@ def get_value(self, dictionary): | |||
# We override the default field access in order to support | |||
# lists in HTML forms. | |||
if html.is_html_input(dictionary): | |||
if self.field_name not in dictionary: | |||
if not any(k.startswith("%s[" % self.field_name) for k in dictionary): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this support alternate field separators, such as field.0
?
@rpkilby Any further thoughts? I know we are one of the few people to post fields and images, though. |
I think you're trying to do the same as in #4763 |
Same as with @awbacker I am updating file fields via a PATCH request with a multi-part form. What work needs to be done to get this merged? I'm willing to help. |
I've created PR #5927 which tackles the problem in another way. I believe my PR fixes your problem (ListField) aswell as my problem (ListSerializer). Your PR does unfortunately not fix the issue for ListSerializer. In addition, your open question about supporting To verify that my PR also solves your problem, I've tried running the tests you provided, which seems to work fine. I have however not included your tests in my PR (though if you want, we can include them). |
@anx-ckreuzberger If this is a better (more elegant) way to solve that, then wonderful. Please just copy/paste the tests over if you feel they have some value; I do, but them I'm partial. I think that list handling was a bit under-tested before. |
The ListField handles html forms data differently than json data. This section
would always return a value (an empty list) for a list field, even when that
field name was not posted at all. This change checks for the field being
present and returns
empty
if it is not; this lets the standard default-value andrequired field handling take over.
[]
)Change was done where it is to make sure everything was clear. There may be a better, more general, location to put it. I wasn't comfortable combining the handling with the first check, because I'm sure there is a reason to call out the
'partial'
condition (for clarity?). One could certainly delete that line, and just use:Suggestions welcome
refs #5807