Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
@adrianholovaty adrianholovaty authored
View
8 django/db/models/fields/__init__.py
@@ -565,6 +565,9 @@ def to_python(self, value):
return value
return smart_unicode(value)
+ def get_prep_value(self, value):
+ return self.to_python(value)
+
def formfield(self, **kwargs):
# Passing max_length to forms.CharField means that the value's length
# will be validated twice. This is considered acceptable since we want
@@ -1006,6 +1009,11 @@ class TextField(Field):
def get_internal_type(self):
return "TextField"
+ def get_prep_value(self, value):
+ if isinstance(value, basestring) or value is None:
+ return value
+ return smart_unicode(value)
+
def formfield(self, **kwargs):
defaults = {'widget': forms.Textarea}
defaults.update(kwargs)
View
4 tests/regressiontests/model_fields/models.py
@@ -55,6 +55,10 @@ class BigInt(models.Model):
value = models.BigIntegerField()
null_value = models.BigIntegerField(null = True, blank = True)
+class Post(models.Model):
+ title = models.CharField(max_length=100)
+ body = models.TextField()
+
###############################################################################
# ImageField
View
15 tests/regressiontests/model_fields/tests.py
@@ -6,7 +6,7 @@
from django.db import models
from django.core.exceptions import ValidationError
-from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt
+from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post
try:
from decimal import Decimal
@@ -227,3 +227,16 @@ def test_coercing(self):
b = BigInt.objects.get(value = '10')
self.assertEqual(b.value, 10)
+class TypeCoercionTests(django.test.TestCase):
+ """
+ Test that database lookups can accept the wrong types and convert
+ them with no error: especially on Postgres 8.3+ which does not do
+ automatic casting at the DB level. See #10015.
+
+ """
+ def test_lookup_integer_in_charfield(self):
+ self.assertEquals(Post.objects.filter(title=9).count(), 0)
+
+ def test_lookup_integer_in_textfield(self):
+ self.assertEquals(Post.objects.filter(body=24).count(), 0)
+
Please sign in to comment.
Something went wrong with that request. Please try again.