Django

Code

Changeset 8143

Show
Ignore:
Timestamp:
07/29/08 19:18:49 (4 months ago)
Author:
mtredinnick
Message:

Fixed #8023 -- Allow filtering of DecimalFields? (in models) using strings.

At the same time, checked all other cases of db_prep_value() to ensure they
aren't making the same mistake.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/fields/__init__.py

    r8139 r8143  
    733733 
    734734    def get_db_prep_value(self, value): 
    735         return connection.ops.value_to_db_decimal(value, self.max_digits
    736                                                   self.decimal_places) 
     735        return connection.ops.value_to_db_decimal(self.to_python(value)
     736                self.max_digits, self.decimal_places) 
    737737 
    738738    def get_manipulator_field_objs(self): 
  • django/trunk/tests/regressiontests/model_fields/models.py

    r8104 r8143  
    22from django.db import models 
    33 
     4try: 
     5    import decimal 
     6except ImportError: 
     7    from django.utils import _decimal as decimal    # Python 2.3 fallback 
     8 
    49class Foo(models.Model): 
    510    a = models.CharField(max_length=10) 
     11    d = models.DecimalField(max_digits=5, decimal_places=3) 
    612 
    713def get_foo(): 
     
    2329                (4,'Fourth'), 
    2430            ) 
    25         ),         
     31        ), 
    2632        (0,'Other'), 
    2733    ) 
    2834    c = models.IntegerField(choices=CHOICES, null=True) 
    29      
     35 
    3036__test__ = {'API_TESTS':""" 
    3137# Create a couple of Places. 
    32 >>> f = Foo.objects.create(a='abc'
     38>>> f = Foo.objects.create(a='abc', d=decimal.Decimal("12.34")
    3339>>> f.id 
    34401 
     
    6874u'' 
    6975 
     76# Regression test for #8023: should be able to filter decimal fields using 
     77# strings (which is what gets passed through from, e.g., the admin interface). 
     78>>> Foo.objects.filter(d=u'1.23') 
     79[] 
     80 
    7081 
    7182"""}