summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2016-04-12 10:14:41 -0400
committerTim Graham <timograham@gmail.com>2016-04-12 10:14:41 -0400
commit1c30a6473df55c7efff71fd24f46c4b8d1ad5630 (patch)
treed9fa882ef90438bcf1885c7113efe61f9bc09289
parent461f74ab19b7b0f393c475a55b4880227a8e4413 (diff)
Refs #22936 -- Moved IntegerField.get_prep_lookup() logic to lookups.
-rw-r--r--django/db/models/fields/__init__.py6
-rw-r--r--django/db/models/lookups.py22
2 files changed, 22 insertions, 6 deletions
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index 812e5425fc..15f4789bbc 100644
--- a/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -5,7 +5,6 @@ import collections
import copy
import datetime
import decimal
-import math
import uuid
import warnings
from base64 import b64decode, b64encode
@@ -1875,11 +1874,6 @@ class IntegerField(Field):
return None
return int(value)
- def get_prep_lookup(self, lookup_type, value):
- if lookup_type in ('gte', 'lt') and isinstance(value, float):
- value = math.ceil(value)
- return super(IntegerField, self).get_prep_lookup(lookup_type, value)
-
def get_internal_type(self):
return "IntegerField"
diff --git a/django/db/models/lookups.py b/django/db/models/lookups.py
index 5634704fed..ad55a0138d 100644
--- a/django/db/models/lookups.py
+++ b/django/db/models/lookups.py
@@ -1,3 +1,4 @@
+import math
import warnings
from copy import copy
@@ -201,6 +202,27 @@ class LessThanOrEqual(BuiltinLookup):
Field.register_lookup(LessThanOrEqual)
+class IntegerFieldFloatRounding(object):
+ """
+ Allow floats to work as query values for IntegerField. Without this, the
+ decimal portion of the float would always be discarded.
+ """
+ def get_prep_lookup(self):
+ if isinstance(self.rhs, float):
+ self.rhs = math.ceil(self.rhs)
+ return super(IntegerFieldFloatRounding, self).get_prep_lookup()
+
+
+class IntegerGreaterThanOrEqual(IntegerFieldFloatRounding, GreaterThanOrEqual):
+ pass
+IntegerField.register_lookup(IntegerGreaterThanOrEqual)
+
+
+class IntegerLessThan(IntegerFieldFloatRounding, LessThan):
+ pass
+IntegerField.register_lookup(IntegerLessThan)
+
+
class In(BuiltinLookup):
lookup_name = 'in'