Django

Code

root/django/trunk/tests/regressiontests/model_fields/tests.py

Revision 8691, 1.6 kB (checked in by mtredinnick, 1 week ago)

Fixed #8101 -- Allow the strings '1' and '0' as filter values for boolean
fields (the latter was causing problems). This allows these values in URLs
(e.g. the admin filtering).

Not an ideal solution to the problem, but will do the job for the time being.

  • Property svn:eol-style set to native
Line 
1 """
2 >>> from django.db.models.fields import *
3 >>> try:
4 ...     from decimal import Decimal
5 ... except ImportError:
6 ...     from django.utils._decimal import Decimal
7
8 # DecimalField
9
10 >>> f = DecimalField(max_digits=4, decimal_places=2)
11
12 >>> f.to_python(3) == Decimal("3")
13 True
14
15 >>> f.to_python("3.14") == Decimal("3.14")
16 True
17
18 >>> f.to_python("abc")
19 Traceback (most recent call last):
20 ...
21 ValidationError: This value must be a decimal number.
22
23 >>> f = DecimalField(max_digits=5, decimal_places=1)
24 >>> x = f.to_python(2)
25 >>> y = f.to_python('2.6')
26
27 >>> f._format(x)
28 u'2.0'
29 >>> f._format(y)
30 u'2.6'
31 >>> f._format(None)
32 >>> f.get_db_prep_lookup('exact', None)
33 [None]
34
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)
41
42 >>> f = TimeField()
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)
47
48 # Boolean and null boolean fields
49 >>> f = BooleanField()
50 >>> for val in (True, '1', 1):
51 ...     f.get_db_prep_lookup('exact', val)
52 [True]
53 [True]
54 [True]
55 >>> for val in (False, '0', 0):
56 ...     f.get_db_prep_lookup('exact', val)
57 [False]
58 [False]
59 [False]
60
61 >>> f = NullBooleanField()
62 >>> for val in (True, '1', 1):
63 ...     f.get_db_prep_lookup('exact', val)
64 [True]
65 [True]
66 [True]
67 >>> for val in (False, '0', 0):
68 ...     f.get_db_prep_lookup('exact', val)
69 [False]
70 [False]
71 [False]
72 >>> f.get_db_prep_lookup('exact', None)
73 [None]
74
75 """
Note: See TracBrowser for help on using the browser.