2
>>> from django.db.models.fields import *
4
... from decimal import Decimal
5
... except ImportError:
6
... from django.utils._decimal import Decimal
10
>>> f = DecimalField(max_digits=4, decimal_places=2)
12
>>> f.to_python(3) == Decimal("3")
15
>>> f.to_python("3.14") == Decimal("3.14")
18
>>> f.to_python("abc")
19
Traceback (most recent call last):
21
ValidationError: This value must be a decimal number.
23
>>> f = DecimalField(max_digits=5, decimal_places=1)
24
>>> x = f.to_python(2)
25
>>> y = f.to_python('2.6')
32
>>> f.get_db_prep_lookup('exact', None)
35
# DateTimeField and TimeField to_python should support usecs:
36
>>> f = DateTimeField()
37
>>> f.to_python('2001-01-02 03:04:05.000006')
38
datetime.datetime(2001, 1, 2, 3, 4, 5, 6)
39
>>> f.to_python('2001-01-02 03:04:05.999999')
40
datetime.datetime(2001, 1, 2, 3, 4, 5, 999999)
43
>>> f.to_python('01:02:03.000004')
44
datetime.time(1, 2, 3, 4)
45
>>> f.to_python('01:02:03.999999')
46
datetime.time(1, 2, 3, 999999)
48
# Boolean and null boolean fields
49
>>> f = BooleanField()
50
>>> for val in (True, '1', 1):
51
... f.get_db_prep_lookup('exact', val)
55
>>> for val in (False, '0', 0):
56
... f.get_db_prep_lookup('exact', val)
61
>>> f = NullBooleanField()
62
>>> for val in (True, '1', 1):
63
... f.get_db_prep_lookup('exact', val)
67
>>> for val in (False, '0', 0):
68
... f.get_db_prep_lookup('exact', val)
72
>>> f.get_db_prep_lookup('exact', None)