-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Fix DictField HTML input returning empty dict for missing fields #9891
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
34065b7
506ff84
31b8e0d
4e1adc1
5afa962
f1e43df
5242d9a
3324ed6
665cf39
dfe6628
e9ebed5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1749,15 +1749,15 @@ def get_value(self, dictionary): | |||||
| # We override the default field access in order to support | ||||||
| # dictionaries in HTML forms. | ||||||
| if html.is_html_input(dictionary): | ||||||
| return html.parse_html_dict(dictionary, prefix=self.field_name) | ||||||
| return html.parse_html_dict(dictionary, prefix=self.field_name, default=empty) | ||||||
| return dictionary.get(self.field_name, empty) | ||||||
|
|
||||||
| def to_internal_value(self, data): | ||||||
| """ | ||||||
| Dicts of native values <- Dicts of primitive datatypes. | ||||||
| """ | ||||||
| if html.is_html_input(data): | ||||||
| data = html.parse_html_dict(data) | ||||||
| data = html.parse_html_dict(data, default=data) | ||||||
|
||||||
| data = html.parse_html_dict(data, default=data) | |
| data = html.parse_html_dict(data, default={}) |
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.
I tried this but it actually breaks test_querydict_dict_input. When to_internal_value gets called, the data is already a MultiValueDict with parsed keys. parse_html_dict with no prefix looks for dot-prefixed keys which don't match, so default={} would lose the already-parsed data. Keeping default=data as a fallback preserves it correctly. Happy to discuss further though!
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,7 +66,7 @@ def parse_html_list(dictionary, prefix='', default=None): | |
| return [ret[item] for item in sorted(ret)] if ret else default | ||
|
|
||
|
|
||
| def parse_html_dict(dictionary, prefix=''): | ||
| def parse_html_dict(dictionary, prefix='', default=None): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks consistent with what we do for parsing html list 👍🏻 : |
||
| """ | ||
|
Comment on lines
+69
to
70
|
||
| Used to support dictionary values in HTML forms. | ||
|
|
||
|
|
@@ -81,6 +81,9 @@ def parse_html_dict(dictionary, prefix=''): | |
| 'email': 'example@example.com' | ||
| } | ||
| } | ||
|
|
||
| :returns a MultiValueDict of the parsed data, or the value specified in | ||
| ``default`` if the dict field was not present in the input | ||
| """ | ||
| ret = MultiValueDict() | ||
| regex = re.compile(r'^%s\.(.+)$' % re.escape(prefix)) | ||
|
|
@@ -92,4 +95,4 @@ def parse_html_dict(dictionary, prefix=''): | |
| value = dictionary.getlist(field) | ||
| ret.setlist(key, value) | ||
|
|
||
| return ret | ||
| return ret if ret else default | ||
Uh oh!
There was an error while loading. Please reload this page.