Ticket #10015: 10015_r12120.diff

File 10015_r12120.diff, 2.5 KB (added by Carl Meyer, 15 years ago)

patch with test and fix at model-field level

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

    diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
    a b  
    565565            return value
    566566        return smart_unicode(value)
    567567
     568    def get_prep_value(self, value):
     569        return self.to_python(value)
     570   
    568571    def formfield(self, **kwargs):
    569572        # Passing max_length to forms.CharField means that the value's length
    570573        # will be validated twice. This is considered acceptable since we want
     
    10061009    def get_internal_type(self):
    10071010        return "TextField"
    10081011
     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
    10091017    def formfield(self, **kwargs):
    10101018        defaults = {'widget': forms.Textarea}
    10111019        defaults.update(kwargs)
  • tests/regressiontests/model_fields/models.py

    diff --git a/tests/regressiontests/model_fields/models.py b/tests/regressiontests/model_fields/models.py
    a b  
    5555    value = models.BigIntegerField()
    5656    null_value = models.BigIntegerField(null = True, blank = True)
    5757
     58class Post(models.Model):
     59    title = models.CharField(max_length=100)
     60    body = models.TextField()
     61   
    5862###############################################################################
    5963# ImageField
    6064
  • tests/regressiontests/model_fields/tests.py

    diff --git a/tests/regressiontests/model_fields/tests.py b/tests/regressiontests/model_fields/tests.py
    a b  
    66from django.db import models
    77from django.core.exceptions import ValidationError
    88
    9 from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt
     9from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post
    1010
    1111try:
    1212    from decimal import Decimal
     
    227227        b = BigInt.objects.get(value = '10')
    228228        self.assertEqual(b.value, 10)
    229229
     230class 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       
Back to Top