4👍
Note this line in the docstring for MultiWidget:
You’ll probably want to use this class with MultiValueField.
That’s the root of your problem. You might be able to get the single-widget-only approach working (Marty says it’s possible in Pro Django, but I’ve never tried it, and I think it’s likely to be more work), but in that case your widget shouldn’t be a subclass of MultiWidget.
What you need to do (if you want to follow the MultiWidget/MultiValueField path) is:
- remove your value_from_datadict method
- define a subclass of MultiValueField with a definition of the compress() method which does the task you’re currently doing in value_from_datadict() (transforming a list of numbers into a datetime.time object)
- set your Widget as the default one for your custom form Field (using the widget class attribute)
- either create a custom model Field which returns your custom form Field from its formfield() method, or use your custom form field manually as a field override in a ModelForm.
Then everything will Just Work.
0👍
I can’t reproduce the problem:
>>> class MyForm(forms.Form):
... t = forms.TimeField(widget=MilitaryTimeWidget())
...
>>> print MyForm(data={'t_0': '13', 't_1': '34'})
******** 13:34:00
<tr><th><label for="id_t_0">T:</label></th><td><select name="t_0" id="id_t_0">
<option value="0">00</option>
[...]
<option value="13" selected="selected">13</option>
[...]
<option value="23">23</option>
</select><select name="t_1" id="id_t_1">
<option value="0">00</option>
[...]
<option value="34" selected="selected">34</option>
[...]
<option value="59">59</option>
</select></td></tr>
Check that your request.POST is correct.
As a sidenote, are you sure this widget gives good usability? Four mouse clicks and possible scrolling of the minutes combobox…
- [Django]-AuthFailed with django-social-auth & Facebook authentication
- [Django]-Django: can't bind an uploaded image to a form ImageField()
- [Django]-Is it possible for a Django template to test for existence of a row in a table without writing a custom tag/filter?
- [Django]-Show management commands in admin site
- [Django]-How to make permission for groups and administrators
Source:stackexchange.com