From 9150fb6b0cb9113ba25cf2c79c0c3cd02d7cf1a0 Mon Sep 17 00:00:00 2001 From: Karen Tracey Date: Sat, 20 Mar 2010 17:56:22 +0000 Subject: [PATCH] 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 --- django/db/models/fields/__init__.py | 6 ++++ tests/regressiontests/queries/models.py | 39 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 260f345fff..9f2a234d23 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -2,6 +2,7 @@ import datetime import decimal import re import time +import math import django.utils.copycompat as copy @@ -882,6 +883,11 @@ class IntegerField(Field): 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" diff --git a/tests/regressiontests/queries/models.py b/tests/regressiontests/queries/models.py index 06c4524d9d..69d990eb7f 100644 --- a/tests/regressiontests/queries/models.py +++ b/tests/regressiontests/queries/models.py @@ -428,6 +428,45 @@ constraints. >>> Number.objects.filter(Q(num__gt=7) & Q(num__lt=12) | Q(num__lt=4)) [] +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.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.objects.filter(num__lt=12.0) +[, ] +>>> Number.objects.filter(num__lt=12.1) +[, , ] +>>> Number.objects.filter(num__gte=11.9) +[] +>>> Number.objects.filter(num__gte=12) +[] +>>> Number.objects.filter(num__gte=12.0) +[] +>>> Number.objects.filter(num__gte=12.1) +[] +>>> Number.objects.filter(num__gte=12.9) +[] +>>> Number.objects.filter(num__lte=11.9) +[, ] +>>> Number.objects.filter(num__lte=12) +[, , ] +>>> Number.objects.filter(num__lte=12.0) +[, , ] +>>> Number.objects.filter(num__lte=12.1) +[, , ] +>>> Number.objects.filter(num__lte=12.9) +[, , ] + Bug #7872 Another variation on the disjunctive filtering theme.