-
-
Notifications
You must be signed in to change notification settings - Fork 131
Closed
Description
If I have a form class like this:
class LeaveForm(forms.ModelForm):
class Meta:
model = Leave
fields = ["date", "type", "portion", "comment"]
where "type" and "portion" are ChoiceField.
I have a Unicorn view like this:
class LeaveFormView(UnicornView):
form_class = LeaveForm
date: date = None
type: str = ""
portion: str = ""
comment: str = ""
If I select an option with this template:
<select id="inputState"
u:model="portion">
<option value="ALL">ALL</option>
</select>
I expect portion to be "ALL". However, I get ["ALL"] instead. This causes validation error.
After some debugging, I discovered that this line in unicorn_view.py line 468 has this call:
value = field.widget.format_value(cleaned_value)
which in turn calls ChoiceWidget.format_value which returns a list. I presume this gets propagated to the front-end and returns as a list.
I'm not sure of how Unicorn works, but I presume potential solution to this would include:
- Special case treatment for
ChoiceFieldandTypedChoiceField. Both of the above usesSelectwidget which inherits fromChoiceWidget. You could make a special conditional for these two fields to coerce list into a basic type. - Alternatively,
ChoiceFieldhas aallow_multiple_selectedattribute. You could use that to decide whether to keep list or not.
Thanks again for this amazing project.
-T
Metadata
Metadata
Assignees
Labels
No labels