Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #10015 -- PostgreSQL 8.3+ no longer barfs when passing an integ…

…er as a filter() value for a CharField or TextField. Thanks, carljm

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12150 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 18723e6e24a08579082ed308e417bc1cc52aa407 1 parent 1d61a1e
Adrian Holovaty authored January 09, 2010
8  django/db/models/fields/__init__.py
@@ -565,6 +565,9 @@ def to_python(self, value):
565 565
             return value
566 566
         return smart_unicode(value)
567 567
 
  568
+    def get_prep_value(self, value):
  569
+        return self.to_python(value)
  570
+    
568 571
     def formfield(self, **kwargs):
569 572
         # Passing max_length to forms.CharField means that the value's length
570 573
         # will be validated twice. This is considered acceptable since we want
@@ -1006,6 +1009,11 @@ class TextField(Field):
1006 1009
     def get_internal_type(self):
1007 1010
         return "TextField"
1008 1011
 
  1012
+    def get_prep_value(self, value):
  1013
+        if isinstance(value, basestring) or value is None:
  1014
+            return value
  1015
+        return smart_unicode(value)
  1016
+
1009 1017
     def formfield(self, **kwargs):
1010 1018
         defaults = {'widget': forms.Textarea}
1011 1019
         defaults.update(kwargs)
4  tests/regressiontests/model_fields/models.py
@@ -55,6 +55,10 @@ class BigInt(models.Model):
55 55
     value = models.BigIntegerField()
56 56
     null_value = models.BigIntegerField(null = True, blank = True)
57 57
 
  58
+class Post(models.Model):
  59
+    title = models.CharField(max_length=100)
  60
+    body = models.TextField()
  61
+    
58 62
 ###############################################################################
59 63
 # ImageField
60 64
 
15  tests/regressiontests/model_fields/tests.py
@@ -6,7 +6,7 @@
6 6
 from django.db import models
7 7
 from django.core.exceptions import ValidationError
8 8
 
9  
-from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt
  9
+from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post
10 10
 
11 11
 try:
12 12
     from decimal import Decimal
@@ -227,3 +227,16 @@ def test_coercing(self):
227 227
         b = BigInt.objects.get(value = '10')
228 228
         self.assertEqual(b.value, 10)
229 229
 
  230
+class TypeCoercionTests(django.test.TestCase):
  231
+    """
  232
+    Test that database lookups can accept the wrong types and convert
  233
+    them with no error: especially on Postgres 8.3+ which does not do
  234
+    automatic casting at the DB level. See #10015.
  235
+
  236
+    """
  237
+    def test_lookup_integer_in_charfield(self):
  238
+        self.assertEquals(Post.objects.filter(title=9).count(), 0)
  239
+        
  240
+    def test_lookup_integer_in_textfield(self):
  241
+        self.assertEquals(Post.objects.filter(body=24).count(), 0)
  242
+        

0 notes on commit 18723e6

Please sign in to comment.
Something went wrong with that request. Please try again.