Skip to content

Commit acf688b

Browse files
NatghoCopilot
andauthored
Update rest_framework/fields.py
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent b0066ab commit acf688b

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

rest_framework/fields.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,12 +1683,18 @@ def get_value(self, dictionary):
16831683
# We override the default field access in order to support
16841684
# lists in HTML forms.
16851685
if html.is_html_input(dictionary):
1686-
# First, try to get the value using the plain field name with getlist
1686+
# First, try to get the value using the plain field name with getlist.
16871687
# This handles standard HTML form list submissions like:
16881688
# <select multiple name="field"><option value="a">...
1689-
val = dictionary.getlist(self.field_name, [])
1690-
if len(val) > 0:
1691-
# Support QueryDict lists in HTML input.
1689+
try:
1690+
# Call getlist with a single argument to support duck-typed MultiDicts
1691+
# that do not accept a default parameter.
1692+
val = dictionary.getlist(self.field_name)
1693+
except (TypeError, KeyError, AttributeError):
1694+
# Fall back to treating the value as not provided.
1695+
val = []
1696+
if val:
1697+
# Support QueryDict lists and other list-like results in HTML input.
16921698
return val
16931699
# For partial updates, avoid calling parse_html_list unless indexed keys are present.
16941700
# This reduces unnecessary parsing overhead for omitted list fields.

0 commit comments

Comments
 (0)