Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Karen Tracey authored March 20, 2010
6  django/db/models/fields/__init__.py
@@ -2,6 +2,7 @@
2 2
 import decimal
3 3
 import re
4 4
 import time
  5
+import math
5 6
 
6 7
 import django.utils.copycompat as copy
7 8
 
@@ -882,6 +883,11 @@ def get_prep_value(self, value):
882 883
             return None
883 884
         return int(value)
884 885
 
  886
+    def get_prep_lookup(self, lookup_type, value):
  887
+        if lookup_type == 'gte' or lookup_type == 'lt':
  888
+            value = math.ceil(value)
  889
+        return super(IntegerField, self).get_prep_lookup(lookup_type, value)
  890
+
885 891
     def get_internal_type(self):
886 892
         return "IntegerField"
887 893
 
39  tests/regressiontests/queries/models.py
@@ -428,6 +428,45 @@ def __unicode__(self):
428 428
 >>> Number.objects.filter(Q(num__gt=7) & Q(num__lt=12) | Q(num__lt=4))
429 429
 [<Number: 8>]
430 430
 
  431
+Bug #12239
  432
+Float was being rounded to integer on gte queries on integer field.  Tests
  433
+show that gt, lt, gte, and lte work as desired.  Note that the fix changes
  434
+get_prep_lookup for gte and lt queries only.
  435
+>>> Number.objects.filter(num__gt=11.9)
  436
+[<Number: 12>]
  437
+>>> Number.objects.filter(num__gt=12)
  438
+[]
  439
+>>> Number.objects.filter(num__gt=12.0)
  440
+[]
  441
+>>> Number.objects.filter(num__gt=12.1)
  442
+[]
  443
+>>> Number.objects.filter(num__lt=12)
  444
+[<Number: 4>, <Number: 8>]
  445
+>>> Number.objects.filter(num__lt=12.0)
  446
+[<Number: 4>, <Number: 8>]
  447
+>>> Number.objects.filter(num__lt=12.1)
  448
+[<Number: 4>, <Number: 8>, <Number: 12>]
  449
+>>> Number.objects.filter(num__gte=11.9)
  450
+[<Number: 12>]
  451
+>>> Number.objects.filter(num__gte=12)
  452
+[<Number: 12>]
  453
+>>> Number.objects.filter(num__gte=12.0)
  454
+[<Number: 12>]
  455
+>>> Number.objects.filter(num__gte=12.1)
  456
+[]
  457
+>>> Number.objects.filter(num__gte=12.9)
  458
+[]
  459
+>>> Number.objects.filter(num__lte=11.9)
  460
+[<Number: 4>, <Number: 8>]
  461
+>>> Number.objects.filter(num__lte=12)
  462
+[<Number: 4>, <Number: 8>, <Number: 12>]
  463
+>>> Number.objects.filter(num__lte=12.0)
  464
+[<Number: 4>, <Number: 8>, <Number: 12>]
  465
+>>> Number.objects.filter(num__lte=12.1)
  466
+[<Number: 4>, <Number: 8>, <Number: 12>]
  467
+>>> Number.objects.filter(num__lte=12.9)
  468
+[<Number: 4>, <Number: 8>, <Number: 12>]
  469
+
431 470
 Bug #7872
432 471
 Another variation on the disjunctive filtering theme.
433 472
 

0 notes on commit 9150fb6

Please sign in to comment.
Something went wrong with that request. Please try again.