當前位置: 首頁>>代碼示例>>Python>>正文


Python forms.DecimalField類代碼示例

本文整理匯總了Python中django.forms.DecimalField的典型用法代碼示例。如果您正苦於以下問題:Python DecimalField類的具體用法?Python DecimalField怎麽用?Python DecimalField使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了DecimalField類的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_decimalfield_scientific

 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'))
開發者ID:mattmcc,項目名稱:django,代碼行數:7,代碼來源:test_decimalfield.py

示例2: __init__

    def __init__(self, currency_widget=None, currency_choices=CURRENCY_CHOICES,
                 choices=CURRENCY_CHOICES, max_value=None, min_value=None,
                 max_digits=None, decimal_places=None, *args, **kwargs):

        # choices does not make sense in this context, it would mean we had
        # to replace two widgets with one widget dynamically... which is a
        # mess. Instead, we let currency_choices be the same as choices and
        # raise a warning.
        if currency_choices != CURRENCY_CHOICES:
            warn('currency_choices will be deprecated in favor of choices', PendingDeprecationWarning)
            choices = currency_choices

        amount_field = DecimalField(max_value, min_value, max_digits, decimal_places, *args, **kwargs)
        currency_field = ChoiceField(choices=choices)

        if VERSION < (1, 10) and hasattr(amount_field, '_has_changed') and hasattr(currency_field, '_has_changed'):
            amount_field.has_changed = amount_field._has_changed
            currency_field.has_changed = currency_field._has_changed

        # TODO: No idea what currency_widget is supposed to do since it doesn't
        # even receive currency choices as input. Somehow it's supposed to be
        # instantiated from outside. Hard to tell.
        if currency_widget:
            self.widget = currency_widget
        else:
            self.widget = MoneyWidget(
                amount_widget=amount_field.widget,
                currency_widget=currency_field.widget
            )

        # The two fields that this widget comprises
        fields = (amount_field, currency_field)
        super(MoneyField, self).__init__(fields, *args, **kwargs)
開發者ID:Suitcake,項目名稱:django-money,代碼行數:33,代碼來源:fields.py

示例3: test_decimalfield_2

 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)
開發者ID:2015E8007361074,項目名稱:django,代碼行數:9,代碼來源:test_decimalfield.py

示例4: test_decimalfield_changed

    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))
開發者ID:2015E8007361074,項目名稱:django,代碼行數:10,代碼來源:test_decimalfield.py

示例5: test_decimalfield_widget_attrs

 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 />')
開發者ID:2015E8007361074,項目名稱:django,代碼行數:12,代碼來源:test_decimalfield.py

示例6: test_enter_a_number_error

 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)
開發者ID:mattmcc,項目名稱:django,代碼行數:12,代碼來源:test_decimalfield.py

示例7: test_decimalfield_5

 def test_decimalfield_5(self):
     f = DecimalField(max_digits=3)
     # Leading whole zeros "collapse" to one digit.
     self.assertEqual(f.clean('0000000.10'), decimal.Decimal("0.1"))
     # But a leading 0 before the . doesn't count towards max_digits
     self.assertEqual(f.clean('0000000.100'), decimal.Decimal("0.100"))
     # Only leading whole zeros "collapse" to one digit.
     self.assertEqual(f.clean('000000.02'), decimal.Decimal('0.02'))
     with self.assertRaisesMessage(ValidationError, "'Ensure that there are no more than 3 digits in total.'"):
         f.clean('000000.0002')
     self.assertEqual(f.clean('.002'), decimal.Decimal("0.002"))
開發者ID:2015E8007361074,項目名稱:django,代碼行數:11,代碼來源:test_decimalfield.py

示例8: test_decimalfield_3

 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" />')
     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'))
開發者ID:277800076,項目名稱:django,代碼行數:19,代碼來源:test_decimalfield.py

示例9: test_decimalfield_4

 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')
開發者ID:2015E8007361074,項目名稱:django,代碼行數:4,代碼來源:test_decimalfield.py

示例10: test_decimalfield_1

 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"))
     with self.assertRaisesMessage(ValidationError, "'Enter a number.'"):
         f.clean('NaN')
     with self.assertRaisesMessage(ValidationError, "'Enter a number.'"):
         f.clean('Inf')
     with self.assertRaisesMessage(ValidationError, "'Enter a number.'"):
         f.clean('-Inf')
     with self.assertRaisesMessage(ValidationError, "'Enter a number.'"):
         f.clean('a')
     with self.assertRaisesMessage(ValidationError, "'Enter a number.'"):
         f.clean('łąść')
     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, "'Enter a number.'"):
         f.clean('1.0a')
     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')
     with self.assertRaisesMessage(ValidationError, "'Enter a number.'"):
         f.clean('--0.12')
     self.assertEqual(f.max_digits, 4)
     self.assertEqual(f.decimal_places, 2)
     self.assertIsNone(f.max_value)
     self.assertIsNone(f.min_value)
開發者ID:2015E8007361074,項目名稱:django,代碼行數:51,代碼來源:test_decimalfield.py

示例11: test_decimalfield_6

 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')
開發者ID:2015E8007361074,項目名稱:django,代碼行數:6,代碼來源:test_decimalfield.py

示例12: test_decimalfield_support_thousands_separator

 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')
開發者ID:mattmcc,項目名稱:django,代碼行數:6,代碼來源:test_decimalfield.py

示例13: test_decimalfield_support_decimal_separator

 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"))
開發者ID:mattmcc,項目名稱:django,代碼行數:4,代碼來源:test_decimalfield.py


注:本文中的django.forms.DecimalField類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。