Ticket #10015: 10015_11X.diff

File 10015_11X.diff, 2.6 KB (added by Karen Tracey, 14 years ago)

Possible fix for 1.1.X branch

  • django/db/models/fields/__init__.py

     
    435435                    ugettext_lazy("This field cannot be null."))
    436436        return smart_unicode(value)
    437437
     438    def get_db_prep_value(self, value):
     439        return self.to_python(value)
     440   
    438441    def formfield(self, **kwargs):
    439442        defaults = {'max_length': self.max_length}
    440443        defaults.update(kwargs)
     
    833836    def get_internal_type(self):
    834837        return "TextField"
    835838
     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
    836844    def formfield(self, **kwargs):
    837845        defaults = {'widget': forms.Textarea}
    838846        defaults.update(kwargs)
  • tests/regressiontests/model_fields/tests.py

     
    66from django.db import models
    77from django.core.exceptions import ValidationError
    88
    9 from models import Foo, Bar, Whiz, BigD, BigS, Image
     9from models import Foo, Bar, Whiz, BigD, BigS, Image, Post
    1010
    1111try:
    1212    from decimal import Decimal
     
    144144        bs = BigS.objects.create(s = 'slug'*50)
    145145        bs = BigS.objects.get(pk=bs.pk)
    146146        self.assertEqual(bs.s, 'slug'*50)
     147
     148class 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       
  • tests/regressiontests/model_fields/models.py

     
    5252    s = models.SlugField(max_length=255)
    5353
    5454
     55class Post(models.Model):
     56    title = models.CharField(max_length=100)
     57    body = models.TextField()
     58   
    5559###############################################################################
    5660# ImageField
    5761
Back to Top