0

我有一个简单的自定义 NumberField:

class NumberInput(forms.widgets.Input):
    input_type = 'number'

class NumberField(forms.DecimalField):
    def __init__(self, *args, **kwargs):
        self.widget = kwargs.get('widget', NumberInput)
        self.min_value = kwargs.get('min', 0)
        self.max_value = kwargs.get('max', 10)
        self.step = kwargs.pop('step', 1)
        super(NumberField, self).__init__(*args, **kwargs)
    def widget_attrs(self, widget):
        attrs = super(NumberField, self).widget_attrs(widget)
        if self.min_value is not None:
            attrs['min'] = self.min_value
        if self.max_value is not None:
            attrs['max'] = self.max_value
        if self.step is not None:
            attrs['step'] = self.step
        return attrs

我以这样的形式使用它:

class TimeLog
     time = NumberField(min_value = '0', max_value = '100000', step = '0.25')

每次我尝试提交它时,无论输入如何,我都会收到相同的错误:

Ensure this value is greater than or equal to 0.

页面上的元素肯定有一个有效值:

<input step="0.25" name="time" min="0" max="100000" type="number" id="id_time" value="0.25">

是什么导致了这种失效?

4

1 回答 1

1

您使用最小值和最大值的字符串而不是整数对其进行初始化。

于 2012-06-19T18:04:50.797 回答