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


Python DecimalField.clean方法代碼示例

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


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

示例1: test_decimalfield_scientific

# 需要導入模塊: from django.forms import DecimalField [as 別名]
# 或者: from django.forms.DecimalField import clean [as 別名]
 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,代碼行數:9,代碼來源:test_decimalfield.py

示例2: test_decimalfield_2

# 需要導入模塊: from django.forms import DecimalField [as 別名]
# 或者: from django.forms.DecimalField import clean [as 別名]
 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,代碼行數:11,代碼來源:test_decimalfield.py

示例3: test_enter_a_number_error

# 需要導入模塊: from django.forms import DecimalField [as 別名]
# 或者: from django.forms.DecimalField import clean [as 別名]
 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,代碼行數:14,代碼來源:test_decimalfield.py

示例4: test_decimalfield_5

# 需要導入模塊: from django.forms import DecimalField [as 別名]
# 或者: from django.forms.DecimalField import clean [as 別名]
 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,代碼行數:13,代碼來源:test_decimalfield.py

示例5: test_decimalfield_3

# 需要導入模塊: from django.forms import DecimalField [as 別名]
# 或者: from django.forms.DecimalField import clean [as 別名]
 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,代碼行數:21,代碼來源:test_decimalfield.py

示例6: test_decimalfield_4

# 需要導入模塊: from django.forms import DecimalField [as 別名]
# 或者: from django.forms.DecimalField import clean [as 別名]
 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,代碼行數:6,代碼來源:test_decimalfield.py

示例7: test_decimalfield_1

# 需要導入模塊: from django.forms import DecimalField [as 別名]
# 或者: from django.forms.DecimalField import clean [as 別名]
 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,代碼行數:53,代碼來源:test_decimalfield.py

示例8: test_decimalfield_6

# 需要導入模塊: from django.forms import DecimalField [as 別名]
# 或者: from django.forms.DecimalField import clean [as 別名]
 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,代碼行數:8,代碼來源:test_decimalfield.py

示例9: test_decimalfield_support_thousands_separator

# 需要導入模塊: from django.forms import DecimalField [as 別名]
# 或者: from django.forms.DecimalField import clean [as 別名]
 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,代碼行數:8,代碼來源:test_decimalfield.py

示例10: test_decimalfield_support_decimal_separator

# 需要導入模塊: from django.forms import DecimalField [as 別名]
# 或者: from django.forms.DecimalField import clean [as 別名]
 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,代碼行數:6,代碼來源:test_decimalfield.py


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