Python django.forms.DecimalField() Examples
The following are 30
code examples of django.forms.DecimalField().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django.forms
, or try the search function
.
Example #1
Source File: forms.py From mrs with GNU Affero General Public License v3.0 | 6 votes |
def to_python(self, value): comma_index = 0 if value: # First we check if there is a comma inside the decimal input, and # store its position try: comma_index = value.index(',') except ValueError: pass # If we found a comma inside the input value, we replace it with a # regular dot if comma_index: value = value.replace(',', '.') # We finally pass the value (modified or not) to the standard # DecimalField return super(AllowedCommaDecimalField, self).to_python(value)
Example #2
Source File: test_decimalfield.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
def test_decimalfield_3(self): f = DecimalField( max_digits=4, decimal_places=2, max_value=decimal.Decimal('1.5'), min_value=decimal.Decimal('0.5') ) self.assertWidgetRendersTo( f, '<input step="0.01" name="f" min="0.5" max="1.5" type="number" id="id_f" required>', ) with self.assertRaisesMessage(ValidationError, "'Ensure this value is less than or equal to 1.5.'"): f.clean('1.6') with self.assertRaisesMessage(ValidationError, "'Ensure this value is greater than or equal to 0.5.'"): f.clean('0.4') self.assertEqual(f.clean('1.5'), decimal.Decimal("1.5")) self.assertEqual(f.clean('0.5'), decimal.Decimal("0.5")) self.assertEqual(f.clean('.5'), decimal.Decimal("0.5")) self.assertEqual(f.clean('00.50'), decimal.Decimal("0.50")) self.assertEqual(f.max_digits, 4) self.assertEqual(f.decimal_places, 2) self.assertEqual(f.max_value, decimal.Decimal('1.5')) self.assertEqual(f.min_value, decimal.Decimal('0.5'))
Example #3
Source File: test_decimalfield.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
def test_decimalfield_3(self): f = DecimalField( max_digits=4, decimal_places=2, max_value=decimal.Decimal('1.5'), min_value=decimal.Decimal('0.5') ) self.assertWidgetRendersTo( f, '<input step="0.01" name="f" min="0.5" max="1.5" type="number" id="id_f" required>', ) with self.assertRaisesMessage(ValidationError, "'Ensure this value is less than or equal to 1.5.'"): f.clean('1.6') with self.assertRaisesMessage(ValidationError, "'Ensure this value is greater than or equal to 0.5.'"): f.clean('0.4') self.assertEqual(f.clean('1.5'), decimal.Decimal("1.5")) self.assertEqual(f.clean('0.5'), decimal.Decimal("0.5")) self.assertEqual(f.clean('.5'), decimal.Decimal("0.5")) self.assertEqual(f.clean('00.50'), decimal.Decimal("0.50")) self.assertEqual(f.max_digits, 4) self.assertEqual(f.decimal_places, 2) self.assertEqual(f.max_value, decimal.Decimal('1.5')) self.assertEqual(f.min_value, decimal.Decimal('0.5'))
Example #4
Source File: registration.py From byro with Apache License 2.0 | 6 votes |
def build_default_field(self, field, model): choices = getattr(field, "choices", None) if choices: return forms.ChoiceField( required=False, label=_("Default value"), choices=[(None, "-----------")] + list(choices), ) if not (model is Member and field.name == "number"): if isinstance(field, models.CharField): return forms.CharField(required=False, label=_("Default value")) elif isinstance(field, models.DecimalField): return forms.DecimalField( required=False, label=_("Default value"), max_digits=field.max_digits, decimal_places=field.decimal_places, ) elif isinstance(field, models.DateField): return forms.CharField(required=False, label=_("Other/fixed date"))
Example #5
Source File: test_decimalfield.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_decimalfield_support_decimal_separator(self): f = DecimalField(localize=True) self.assertEqual(f.clean('1001,10'), decimal.Decimal("1001.10")) self.assertEqual(f.clean('1001.10'), decimal.Decimal("1001.10"))
Example #6
Source File: test_decimalfield.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_decimalfield_6(self): f = DecimalField(max_digits=2, decimal_places=2) self.assertEqual(f.clean('.01'), decimal.Decimal(".01")) msg = "'Ensure that there are no more than 0 digits before the decimal point.'" with self.assertRaisesMessage(ValidationError, msg): f.clean('1.1')
Example #7
Source File: fields.py From esdc-ce with Apache License 2.0 | 5 votes |
def validate(self, value): super(DecimalField, self).validate(value) if value in validators.EMPTY_VALUES: return # Check for NaN, Inf and -Inf values. We can't compare directly for NaN, # since it is never equal to itself. However, NaN is the only value that # isn't equal to itself, so we can use this to identify NaN if value != value or value == Decimal("Inf") or value == Decimal("-Inf"): raise ValidationError(self.error_messages['invalid']) sign, digittuple, exponent = value.as_tuple() decimals = abs(exponent) # digittuple doesn't include any leading zeros. digits = len(digittuple) if decimals > digits: # We have leading zeros up to or past the decimal point. Count # everything past the decimal point as a digit. We do not count # 0 before the decimal point as a digit since that would mean # we would not allow max_digits = decimal_places. digits = decimals whole_digits = digits - decimals if self.max_digits is not None and digits > self.max_digits: raise ValidationError(self.error_messages['max_digits'] % self.max_digits) if self.decimal_places is not None and decimals > self.decimal_places: raise ValidationError(self.error_messages['max_decimal_places'] % self.decimal_places) if (self.max_digits is not None and self.decimal_places is not None and whole_digits > (self.max_digits - self.decimal_places)): raise ValidationError(self.error_messages['max_whole_digits'] % (self.max_digits - self.decimal_places)) return value
Example #8
Source File: fields.py From esdc-ce with Apache License 2.0 | 5 votes |
def __init__(self, max_value=None, min_value=None, max_digits=None, decimal_places=None, *args, **kwargs): self.max_value, self.min_value = max_value, min_value self.max_digits, self.decimal_places = max_digits, decimal_places super(DecimalField, self).__init__(*args, **kwargs) if max_value is not None: self.validators.append(validators.MaxValueValidator(max_value)) if min_value is not None: self.validators.append(validators.MinValueValidator(min_value))
Example #9
Source File: test_decimalfield.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_decimalfield_support_thousands_separator(self): f = DecimalField(localize=True) self.assertEqual(f.clean('1.001,10'), decimal.Decimal("1001.10")) msg = "'Enter a number.'" with self.assertRaisesMessage(ValidationError, msg): f.clean('1,001.1')
Example #10
Source File: test_decimalfield.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_decimalfield_changed(self): f = DecimalField(max_digits=2, decimal_places=2) d = decimal.Decimal("0.1") self.assertFalse(f.has_changed(d, '0.10')) self.assertTrue(f.has_changed(d, '0.101')) with translation.override('fr'), self.settings(USE_L10N=True): f = DecimalField(max_digits=2, decimal_places=2, localize=True) localized_d = formats.localize_input(d) # -> '0,1' in French self.assertFalse(f.has_changed(d, localized_d))
Example #11
Source File: test_decimalfield.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_decimalfield_localized(self): """ A localized DecimalField's widget renders to a text input without number input specific attributes. """ f = DecimalField(localize=True) self.assertWidgetRendersTo(f, '<input id="id_f" name="f" type="text" required>')
Example #12
Source File: test_decimalfield.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_decimalfield_widget_attrs(self): f = DecimalField(max_digits=6, decimal_places=2) self.assertEqual(f.widget_attrs(Widget()), {}) self.assertEqual(f.widget_attrs(NumberInput()), {'step': '0.01'}) f = DecimalField(max_digits=10, decimal_places=0) self.assertEqual(f.widget_attrs(NumberInput()), {'step': '1'}) f = DecimalField(max_digits=19, decimal_places=19) self.assertEqual(f.widget_attrs(NumberInput()), {'step': '1e-19'}) f = DecimalField(max_digits=20) self.assertEqual(f.widget_attrs(NumberInput()), {'step': 'any'}) f = DecimalField(max_digits=6, widget=NumberInput(attrs={'step': '0.01'})) self.assertWidgetRendersTo(f, '<input step="0.01" name="f" type="number" id="id_f" required>')
Example #13
Source File: test_decimalfield.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_decimalfield_scientific(self): f = DecimalField(max_digits=4, decimal_places=2) with self.assertRaisesMessage(ValidationError, "Ensure that there are no more"): f.clean('1E+2') self.assertEqual(f.clean('1E+1'), decimal.Decimal('10')) self.assertEqual(f.clean('1E-1'), decimal.Decimal('0.1')) self.assertEqual(f.clean('0.546e+2'), decimal.Decimal('54.6'))
Example #14
Source File: __init__.py From python2017 with MIT License | 5 votes |
def __init__(self, verbose_name=None, name=None, max_digits=None, decimal_places=None, **kwargs): self.max_digits, self.decimal_places = max_digits, decimal_places super(DecimalField, self).__init__(verbose_name, name, **kwargs)
Example #15
Source File: test_decimalfield.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_decimalfield_1(self): f = DecimalField(max_digits=4, decimal_places=2) self.assertWidgetRendersTo(f, '<input id="id_f" step="0.01" type="number" name="f" required>') with self.assertRaisesMessage(ValidationError, "'This field is required.'"): f.clean('') with self.assertRaisesMessage(ValidationError, "'This field is required.'"): f.clean(None) self.assertEqual(f.clean('1'), decimal.Decimal("1")) self.assertIsInstance(f.clean('1'), decimal.Decimal) self.assertEqual(f.clean('23'), decimal.Decimal("23")) self.assertEqual(f.clean('3.14'), decimal.Decimal("3.14")) self.assertEqual(f.clean(3.14), decimal.Decimal("3.14")) self.assertEqual(f.clean(decimal.Decimal('3.14')), decimal.Decimal("3.14")) self.assertEqual(f.clean('1.0 '), decimal.Decimal("1.0")) self.assertEqual(f.clean(' 1.0'), decimal.Decimal("1.0")) self.assertEqual(f.clean(' 1.0 '), decimal.Decimal("1.0")) with self.assertRaisesMessage(ValidationError, "'Ensure that there are no more than 4 digits in total.'"): f.clean('123.45') with self.assertRaisesMessage(ValidationError, "'Ensure that there are no more than 2 decimal places.'"): f.clean('1.234') msg = "'Ensure that there are no more than 2 digits before the decimal point.'" with self.assertRaisesMessage(ValidationError, msg): f.clean('123.4') self.assertEqual(f.clean('-12.34'), decimal.Decimal("-12.34")) with self.assertRaisesMessage(ValidationError, "'Ensure that there are no more than 4 digits in total.'"): f.clean('-123.45') self.assertEqual(f.clean('-.12'), decimal.Decimal("-0.12")) self.assertEqual(f.clean('-00.12'), decimal.Decimal("-0.12")) self.assertEqual(f.clean('-000.12'), decimal.Decimal("-0.12")) with self.assertRaisesMessage(ValidationError, "'Ensure that there are no more than 2 decimal places.'"): f.clean('-000.123') with self.assertRaisesMessage(ValidationError, "'Ensure that there are no more than 4 digits in total.'"): f.clean('-000.12345') self.assertEqual(f.max_digits, 4) self.assertEqual(f.decimal_places, 2) self.assertIsNone(f.max_value) self.assertIsNone(f.min_value)
Example #16
Source File: test_decimalfield.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_enter_a_number_error(self): f = DecimalField(max_digits=4, decimal_places=2) values = ( '-NaN', 'NaN', '+NaN', '-sNaN', 'sNaN', '+sNaN', '-Inf', 'Inf', '+Inf', '-Infinity', 'Infinity', '+Infinity', 'a', 'łąść', '1.0a', '--0.12', ) for value in values: with self.subTest(value=value), self.assertRaisesMessage(ValidationError, "'Enter a number.'"): f.clean(value)
Example #17
Source File: test_decimalfield.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_decimalfield_support_thousands_separator(self): f = DecimalField(localize=True) self.assertEqual(f.clean('1.001,10'), decimal.Decimal("1001.10")) msg = "'Enter a number.'" with self.assertRaisesMessage(ValidationError, msg): f.clean('1,001.1')
Example #18
Source File: test_decimalfield.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_decimalfield_widget_attrs(self): f = DecimalField(max_digits=6, decimal_places=2) self.assertEqual(f.widget_attrs(Widget()), {}) self.assertEqual(f.widget_attrs(NumberInput()), {'step': '0.01'}) f = DecimalField(max_digits=10, decimal_places=0) self.assertEqual(f.widget_attrs(NumberInput()), {'step': '1'}) f = DecimalField(max_digits=19, decimal_places=19) self.assertEqual(f.widget_attrs(NumberInput()), {'step': '1e-19'}) f = DecimalField(max_digits=20) self.assertEqual(f.widget_attrs(NumberInput()), {'step': 'any'}) f = DecimalField(max_digits=6, widget=NumberInput(attrs={'step': '0.01'})) self.assertWidgetRendersTo(f, '<input step="0.01" name="f" type="number" id="id_f" required>')
Example #19
Source File: test_decimalfield.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_decimalfield_scientific(self): f = DecimalField(max_digits=4, decimal_places=2) with self.assertRaisesMessage(ValidationError, "Ensure that there are no more"): f.clean('1E+2') self.assertEqual(f.clean('1E+1'), decimal.Decimal('10')) self.assertEqual(f.clean('1E-1'), decimal.Decimal('0.1')) self.assertEqual(f.clean('0.546e+2'), decimal.Decimal('54.6'))
Example #20
Source File: test_decimalfield.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_decimalfield_6(self): f = DecimalField(max_digits=2, decimal_places=2) self.assertEqual(f.clean('.01'), decimal.Decimal(".01")) msg = "'Ensure that there are no more than 0 digits before the decimal point.'" with self.assertRaisesMessage(ValidationError, msg): f.clean('1.1')
Example #21
Source File: test_decimalfield.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_decimalfield_4(self): f = DecimalField(decimal_places=2) with self.assertRaisesMessage(ValidationError, "'Ensure that there are no more than 2 decimal places.'"): f.clean('0.00000001')
Example #22
Source File: test_decimalfield.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_decimalfield_2(self): f = DecimalField(max_digits=4, decimal_places=2, required=False) self.assertIsNone(f.clean('')) self.assertIsNone(f.clean(None)) self.assertEqual(f.clean('1'), decimal.Decimal("1")) self.assertEqual(f.max_digits, 4) self.assertEqual(f.decimal_places, 2) self.assertIsNone(f.max_value) self.assertIsNone(f.min_value)
Example #23
Source File: test_decimalfield.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_enter_a_number_error(self): f = DecimalField(max_digits=4, decimal_places=2) values = ( '-NaN', 'NaN', '+NaN', '-sNaN', 'sNaN', '+sNaN', '-Inf', 'Inf', '+Inf', '-Infinity', 'Infinity', '+Infinity', 'a', 'łąść', '1.0a', '--0.12', ) for value in values: with self.subTest(value=value), self.assertRaisesMessage(ValidationError, "'Enter a number.'"): f.clean(value)
Example #24
Source File: test_decimalfield.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_decimalfield_1(self): f = DecimalField(max_digits=4, decimal_places=2) self.assertWidgetRendersTo(f, '<input id="id_f" step="0.01" type="number" name="f" required>') with self.assertRaisesMessage(ValidationError, "'This field is required.'"): f.clean('') with self.assertRaisesMessage(ValidationError, "'This field is required.'"): f.clean(None) self.assertEqual(f.clean('1'), decimal.Decimal("1")) self.assertIsInstance(f.clean('1'), decimal.Decimal) self.assertEqual(f.clean('23'), decimal.Decimal("23")) self.assertEqual(f.clean('3.14'), decimal.Decimal("3.14")) self.assertEqual(f.clean(3.14), decimal.Decimal("3.14")) self.assertEqual(f.clean(decimal.Decimal('3.14')), decimal.Decimal("3.14")) self.assertEqual(f.clean('1.0 '), decimal.Decimal("1.0")) self.assertEqual(f.clean(' 1.0'), decimal.Decimal("1.0")) self.assertEqual(f.clean(' 1.0 '), decimal.Decimal("1.0")) with self.assertRaisesMessage(ValidationError, "'Ensure that there are no more than 4 digits in total.'"): f.clean('123.45') with self.assertRaisesMessage(ValidationError, "'Ensure that there are no more than 2 decimal places.'"): f.clean('1.234') msg = "'Ensure that there are no more than 2 digits before the decimal point.'" with self.assertRaisesMessage(ValidationError, msg): f.clean('123.4') self.assertEqual(f.clean('-12.34'), decimal.Decimal("-12.34")) with self.assertRaisesMessage(ValidationError, "'Ensure that there are no more than 4 digits in total.'"): f.clean('-123.45') self.assertEqual(f.clean('-.12'), decimal.Decimal("-0.12")) self.assertEqual(f.clean('-00.12'), decimal.Decimal("-0.12")) self.assertEqual(f.clean('-000.12'), decimal.Decimal("-0.12")) with self.assertRaisesMessage(ValidationError, "'Ensure that there are no more than 2 decimal places.'"): f.clean('-000.123') with self.assertRaisesMessage(ValidationError, "'Ensure that there are no more than 4 digits in total.'"): f.clean('-000.12345') self.assertEqual(f.max_digits, 4) self.assertEqual(f.decimal_places, 2) self.assertIsNone(f.max_value) self.assertIsNone(f.min_value)
Example #25
Source File: fields.py From Dailyfresh-B2C with Apache License 2.0 | 5 votes |
def __init__(self, fields=None, *args, **kwargs): if fields is None: fields = ( forms.DecimalField(), forms.DecimalField()) super(RangeField, self).__init__(fields, *args, **kwargs)
Example #26
Source File: test_converter.py From graphene-django with MIT License | 5 votes |
def test_should_decimal_convert_float(): assert_conversion(forms.DecimalField, Float)
Example #27
Source File: __init__.py From python2017 with MIT License | 5 votes |
def formfield(self, **kwargs): defaults = { 'max_digits': self.max_digits, 'decimal_places': self.decimal_places, 'form_class': forms.DecimalField, } defaults.update(kwargs) return super(DecimalField, self).formfield(**defaults)
Example #28
Source File: __init__.py From python2017 with MIT License | 5 votes |
def get_internal_type(self): return "DecimalField"
Example #29
Source File: __init__.py From python2017 with MIT License | 5 votes |
def deconstruct(self): name, path, args, kwargs = super(DecimalField, self).deconstruct() if self.max_digits is not None: kwargs['max_digits'] = self.max_digits if self.decimal_places is not None: kwargs['decimal_places'] = self.decimal_places return name, path, args, kwargs
Example #30
Source File: __init__.py From python2017 with MIT License | 5 votes |
def validators(self): return super(DecimalField, self).validators + [ validators.DecimalValidator(self.max_digits, self.decimal_places) ]