summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKaren Tracey <kmtracey@gmail.com>2010-01-19 16:55:22 +0000
committerKaren Tracey <kmtracey@gmail.com>2010-01-19 16:55:22 +0000
commit46d8ae7c243127a98a7bc1b745ef84d37d82f76c (patch)
treef9c59c4fc652bc55bd30d02c6d12c52f47a1ecc9
parenta8e659c54fee749eb4e3496db75ae225ffb9dfb0 (diff)
[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
-rw-r--r--django/db/models/fields/__init__.py8
-rw-r--r--tests/regressiontests/model_fields/models.py4
-rw-r--r--tests/regressiontests/model_fields/tests.py16
3 files changed, 27 insertions, 1 deletions
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index 55ddda5b1b..2a7c17f876 100644
--- a/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -435,6 +435,9 @@ class CharField(Field):
ugettext_lazy("This field cannot be null."))
return smart_unicode(value)
+ def get_db_prep_value(self, value):
+ return self.to_python(value)
+
def formfield(self, **kwargs):
defaults = {'max_length': self.max_length}
defaults.update(kwargs)
@@ -833,6 +836,11 @@ class TextField(Field):
def get_internal_type(self):
return "TextField"
+ def get_db_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)
diff --git a/tests/regressiontests/model_fields/models.py b/tests/regressiontests/model_fields/models.py
index 81e6c22f09..462e96874b 100644
--- a/tests/regressiontests/model_fields/models.py
+++ b/tests/regressiontests/model_fields/models.py
@@ -52,6 +52,10 @@ class BigS(models.Model):
s = models.SlugField(max_length=255)
+class Post(models.Model):
+ title = models.CharField(max_length=100)
+ body = models.TextField()
+
###############################################################################
# ImageField
diff --git a/tests/regressiontests/model_fields/tests.py b/tests/regressiontests/model_fields/tests.py
index 7a6fee5a2a..29b4af50b7 100644
--- a/tests/regressiontests/model_fields/tests.py
+++ b/tests/regressiontests/model_fields/tests.py
@@ -6,7 +6,7 @@ from django import forms
from django.db import models
from django.core.exceptions import ValidationError
-from models import Foo, Bar, Whiz, BigD, BigS, Image
+from models import Foo, Bar, Whiz, BigD, BigS, Image, Post
try:
from decimal import Decimal
@@ -144,3 +144,17 @@ class SlugFieldTests(django.test.TestCase):
bs = BigS.objects.create(s = 'slug'*50)
bs = BigS.objects.get(pk=bs.pk)
self.assertEqual(bs.s, 'slug'*50)
+
+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)
+