#26104 closed Bug (fixed)
forms.DurationField raise a TypeError when passing it a number
| Reported by: | xialvjun | Owned by: | Sasha Gaevsky |
|---|---|---|---|
| Component: | Forms | Version: | 1.9 |
| Severity: | Normal | Keywords: | forms, TypeError |
| Cc: | django@… | Triage Stage: | Ready for checkin |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
from django.test import TestCase
from django import forms
class JieDuanModePartForm(forms.Form):
duration = forms.DurationField()
class JieDuanModeFormTestCase(TestCase):
def test_data(self):
data = {'duration': 10800}
form = JieDuanModePartForm(data=data)
valid = form.is_valid()
print(valid)
print(form.cleaned_data)
code above raise a TypeError...
in forms/fields.py class DurationField method to_python it use:
value = parse_duration(value)
but if wrap it with try block:
try:
value = parse_duration(value)
except TypeError as e:
raise ValidationError(self.error_messages['invalid'], code='invalid')
things may be better...
Or just value = parse_duration(str(value))
Change History (7)
comment:1 by , 10 years ago
| Cc: | added |
|---|
comment:2 by , 10 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:3 by , 10 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:5 by , 10 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
Note:
See TracTickets
for help on using tickets.
Chiming in: I suggested xialvjun open this, as I believe it's inconsistent with at least some other fields, even though in the naive case all GET/POST data is stringy, other fields have afforded using real, typed dicts, either from JSON deserialization or just as dictionary validation.
Specifically, the following fields already catch TypeError (gracefully, I think) by a quick glance at the source: