Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.1.X] Fixed #10015 -- PostgreSQL 8.3+ no longer barfs when passing …

…an integer as a filter() value for a CharField? or TextField?. Thanks, carljm

Backport of r12150 from trunk.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12263 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 46d8ae7c243127a98a7bc1b745ef84d37d82f76c 1 parent a8e659c
Karen Tracey authored January 19, 2010
8  django/db/models/fields/__init__.py
@@ -435,6 +435,9 @@ def to_python(self, value):
435 435
                     ugettext_lazy("This field cannot be null."))
436 436
         return smart_unicode(value)
437 437
 
  438
+    def get_db_prep_value(self, value):
  439
+        return self.to_python(value)
  440
+    
438 441
     def formfield(self, **kwargs):
439 442
         defaults = {'max_length': self.max_length}
440 443
         defaults.update(kwargs)
@@ -833,6 +836,11 @@ class TextField(Field):
833 836
     def get_internal_type(self):
834 837
         return "TextField"
835 838
 
  839
+    def get_db_prep_value(self, value):
  840
+        if isinstance(value, basestring) or value is None:
  841
+            return value
  842
+        return smart_unicode(value)
  843
+
836 844
     def formfield(self, **kwargs):
837 845
         defaults = {'widget': forms.Textarea}
838 846
         defaults.update(kwargs)
4  tests/regressiontests/model_fields/models.py
@@ -52,6 +52,10 @@ class BigS(models.Model):
52 52
     s = models.SlugField(max_length=255)
53 53
 
54 54
 
  55
+class Post(models.Model):
  56
+    title = models.CharField(max_length=100)
  57
+    body = models.TextField()
  58
+    
55 59
 ###############################################################################
56 60
 # ImageField
57 61
 
16  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
  9
+from models import Foo, Bar, Whiz, BigD, BigS, Image, Post
10 10
 
11 11
 try:
12 12
     from decimal import Decimal
@@ -144,3 +144,17 @@ def test_slugfield_max_length(self):
144 144
         bs = BigS.objects.create(s = 'slug'*50)
145 145
         bs = BigS.objects.get(pk=bs.pk)
146 146
         self.assertEqual(bs.s, 'slug'*50)
  147
+
  148
+class TypeCoercionTests(django.test.TestCase):
  149
+    """
  150
+    Test that database lookups can accept the wrong types and convert
  151
+    them with no error: especially on Postgres 8.3+ which does not do
  152
+    automatic casting at the DB level. See #10015.
  153
+
  154
+    """
  155
+    def test_lookup_integer_in_charfield(self):
  156
+        self.assertEquals(Post.objects.filter(title=9).count(), 0)
  157
+        
  158
+    def test_lookup_integer_in_textfield(self):
  159
+        self.assertEquals(Post.objects.filter(body=24).count(), 0)
  160
+        

0 notes on commit 46d8ae7

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