Code

Ticket #10015: 10015_11X.diff

File 10015_11X.diff, 2.6 KB (added by kmtracey, 4 years ago)

Possible fix for 1.1.X branch

Line 
1Index: django/db/models/fields/__init__.py
2===================================================================
3--- django/db/models/fields/__init__.py (revision 12262)
4+++ django/db/models/fields/__init__.py (working copy)
5@@ -435,6 +435,9 @@
6                     ugettext_lazy("This field cannot be null."))
7         return smart_unicode(value)
8 
9+    def get_db_prep_value(self, value):
10+        return self.to_python(value)
11+   
12     def formfield(self, **kwargs):
13         defaults = {'max_length': self.max_length}
14         defaults.update(kwargs)
15@@ -833,6 +836,11 @@
16     def get_internal_type(self):
17         return "TextField"
18 
19+    def get_db_prep_value(self, value):
20+        if isinstance(value, basestring) or value is None:
21+            return value
22+        return smart_unicode(value)
23+
24     def formfield(self, **kwargs):
25         defaults = {'widget': forms.Textarea}
26         defaults.update(kwargs)
27Index: tests/regressiontests/model_fields/tests.py
28===================================================================
29--- tests/regressiontests/model_fields/tests.py (revision 12262)
30+++ tests/regressiontests/model_fields/tests.py (working copy)
31@@ -6,7 +6,7 @@
32 from django.db import models
33 from django.core.exceptions import ValidationError
34 
35-from models import Foo, Bar, Whiz, BigD, BigS, Image
36+from models import Foo, Bar, Whiz, BigD, BigS, Image, Post
37 
38 try:
39     from decimal import Decimal
40@@ -144,3 +144,17 @@
41         bs = BigS.objects.create(s = 'slug'*50)
42         bs = BigS.objects.get(pk=bs.pk)
43         self.assertEqual(bs.s, 'slug'*50)
44+
45+class TypeCoercionTests(django.test.TestCase):
46+    """
47+    Test that database lookups can accept the wrong types and convert
48+    them with no error: especially on Postgres 8.3+ which does not do
49+    automatic casting at the DB level. See #10015.
50+
51+    """
52+    def test_lookup_integer_in_charfield(self):
53+        self.assertEquals(Post.objects.filter(title=9).count(), 0)
54+       
55+    def test_lookup_integer_in_textfield(self):
56+        self.assertEquals(Post.objects.filter(body=24).count(), 0)
57+       
58Index: tests/regressiontests/model_fields/models.py
59===================================================================
60--- tests/regressiontests/model_fields/models.py        (revision 12262)
61+++ tests/regressiontests/model_fields/models.py        (working copy)
62@@ -52,6 +52,10 @@
63     s = models.SlugField(max_length=255)
64 
65 
66+class Post(models.Model):
67+    title = models.CharField(max_length=100)
68+    body = models.TextField()
69+   
70 ###############################################################################
71 # ImageField
72