Fixed #12239: Fixed results of gte and lt queries when comparing floats to integer field values.

Thanks waverider, aroy, SmileyChris, and pmclanahan.



git-svn-id: http://code.djangoproject.com/svn/django/trunk@12819 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Karen Tracey 2010-03-20 17:56:22 +00:00
parent 3a5dae5ed3
commit 9150fb6b0c
2 changed files with 45 additions and 0 deletions

View File

@ -2,6 +2,7 @@ import datetime
import decimal import decimal
import re import re
import time import time
import math
import django.utils.copycompat as copy import django.utils.copycompat as copy
@ -882,6 +883,11 @@ class IntegerField(Field):
return None return None
return int(value) return int(value)
def get_prep_lookup(self, lookup_type, value):
if lookup_type == 'gte' or lookup_type == 'lt':
value = math.ceil(value)
return super(IntegerField, self).get_prep_lookup(lookup_type, value)
def get_internal_type(self): def get_internal_type(self):
return "IntegerField" return "IntegerField"

View File

@ -428,6 +428,45 @@ constraints.
>>> Number.objects.filter(Q(num__gt=7) & Q(num__lt=12) | Q(num__lt=4)) >>> Number.objects.filter(Q(num__gt=7) & Q(num__lt=12) | Q(num__lt=4))
[<Number: 8>] [<Number: 8>]
Bug #12239
Float was being rounded to integer on gte queries on integer field. Tests
show that gt, lt, gte, and lte work as desired. Note that the fix changes
get_prep_lookup for gte and lt queries only.
>>> Number.objects.filter(num__gt=11.9)
[<Number: 12>]
>>> Number.objects.filter(num__gt=12)
[]
>>> Number.objects.filter(num__gt=12.0)
[]
>>> Number.objects.filter(num__gt=12.1)
[]
>>> Number.objects.filter(num__lt=12)
[<Number: 4>, <Number: 8>]
>>> Number.objects.filter(num__lt=12.0)
[<Number: 4>, <Number: 8>]
>>> Number.objects.filter(num__lt=12.1)
[<Number: 4>, <Number: 8>, <Number: 12>]
>>> Number.objects.filter(num__gte=11.9)
[<Number: 12>]
>>> Number.objects.filter(num__gte=12)
[<Number: 12>]
>>> Number.objects.filter(num__gte=12.0)
[<Number: 12>]
>>> Number.objects.filter(num__gte=12.1)
[]
>>> Number.objects.filter(num__gte=12.9)
[]
>>> Number.objects.filter(num__lte=11.9)
[<Number: 4>, <Number: 8>]
>>> Number.objects.filter(num__lte=12)
[<Number: 4>, <Number: 8>, <Number: 12>]
>>> Number.objects.filter(num__lte=12.0)
[<Number: 4>, <Number: 8>, <Number: 12>]
>>> Number.objects.filter(num__lte=12.1)
[<Number: 4>, <Number: 8>, <Number: 12>]
>>> Number.objects.filter(num__lte=12.9)
[<Number: 4>, <Number: 8>, <Number: 12>]
Bug #7872 Bug #7872
Another variation on the disjunctive filtering theme. Another variation on the disjunctive filtering theme.