Django

Code

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

Revision 8143, 1.6 kB (checked in by mtredinnick, 2 months ago)

Fixed #8023 -- Allow filtering of DecimalFields? (in models) using strings.

At the same time, checked all other cases of db_prep_value() to ensure they
aren't making the same mistake.

  • Property svn:eol-style set to native
Line 
1 from django.db import models
2
3 try:
4     import decimal
5 except ImportError:
6     from django.utils import _decimal as decimal    # Python 2.3 fallback
7
8 class Foo(models.Model):
9     a = models.CharField(max_length=10)
10     d = models.DecimalField(max_digits=5, decimal_places=3)
11
12 def get_foo():
13     return Foo.objects.get(id=1)
14
15 class Bar(models.Model):
16     b = models.CharField(max_length=10)
17     a = models.ForeignKey(Foo, default=get_foo)
18
19 class Whiz(models.Model):
20     CHOICES = (
21         ('Group 1', (
22                 (1,'First'),
23                 (2,'Second'),
24             )
25         ),
26         ('Group 2', (
27                 (3,'Third'),
28                 (4,'Fourth'),
29             )
30         ),
31         (0,'Other'),
32     )
33     c = models.IntegerField(choices=CHOICES, null=True)
34
35 __test__ = {'API_TESTS':"""
36 # Create a couple of Places.
37 >>> f = Foo.objects.create(a='abc', d=decimal.Decimal("12.34"))
38 >>> f.id
39 1
40 >>> b = Bar(b = "bcd")
41 >>> b.a
42 <Foo: Foo object>
43 >>> b.save()
44
45 # Regression tests for #7913
46 # Check that get_choices and get_flatchoices interact with
47 # get_FIELD_display to return the expected values.
48
49 # Test a nested value
50 >>> w = Whiz(c=1)
51 >>> w.save()
52 >>> w.get_c_display()
53 u'First'
54
55 # Test a top level value
56 >>> w.c = 0
57 >>> w.get_c_display()
58 u'Other'
59
60 # Test an invalid data value
61 >>> w.c = 9
62 >>> w.get_c_display()
63 9
64
65 # Test a blank data value
66 >>> w.c = None
67 >>> print w.get_c_display()
68 None
69
70 # Test an empty data value
71 >>> w.c = ''
72 >>> w.get_c_display()
73 u''
74
75 # Regression test for #8023: should be able to filter decimal fields using
76 # strings (which is what gets passed through from, e.g., the admin interface).
77 >>> Foo.objects.filter(d=u'1.23')
78 []
79
80
81 """}
Note: See TracBrowser for help on using the browser.