Ticket #12239: django-bug-12239.diff

File django-bug-12239.diff, 2.3 KB (added by Paul McLanahan, 15 years ago)

Updated previous patch with suggestions and added tests.

  • django/db/models/fields/__init__.py

     
    22import decimal
    33import re
    44import time
     5import math
    56
    67import django.utils.copycompat as copy
    78
     
    882883            return None
    883884        return int(value)
    884885
     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
    885891    def get_internal_type(self):
    886892        return "IntegerField"
    887893
  • tests/regressiontests/queries/models.py

     
    428428>>> Number.objects.filter(Q(num__gt=7) & Q(num__lt=12) | Q(num__lt=4))
    429429[<Number: 8>]
    430430
     431Bug #12239
     432Float was being rounded to integer on gte queries on integer field.  Tests
     433show that gt, lt, gte, and lte work as desired.  Note that the fix changes
     434get_prep_lookup for gte 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
    431470Bug #7872
    432471Another variation on the disjunctive filtering theme.
    433472
Back to Top