Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #12239: Fixed results of gte and lt queries when comparing floa…

…ts 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
  • Loading branch information...
commit 9150fb6b0cb9113ba25cf2c79c0c3cd02d7cf1a0 1 parent 3a5dae5
@kmtracey kmtracey authored
View
6 django/db/models/fields/__init__.py
@@ -2,6 +2,7 @@
import decimal
import re
import time
+import math
import django.utils.copycompat as copy
@@ -882,6 +883,11 @@ def get_prep_value(self, value):
return None
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):
return "IntegerField"
View
39 tests/regressiontests/queries/models.py
@@ -428,6 +428,45 @@ def __unicode__(self):
>>> Number.objects.filter(Q(num__gt=7) & Q(num__lt=12) | Q(num__lt=4))
[<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
Another variation on the disjunctive filtering theme.
Please sign in to comment.
Something went wrong with that request. Please try again.