Dear Django list,

using Django 1.3 with Python 2.6.5 on Ubuntu 10.04 and mod_wsgi, with Oracle database, I've just experienced a case where the use of select_related() changes the result type of a DecimalField in a related object from decimal.Decimal to float (which in turn breaks my application).


In detail, please consider the following models (unrelated fields omitted for clarity):


class Code(models.Model):
    id        = models.BigIntegerField(primary_key=True)
grenzwert = models.DecimalField(null=True, max_digits=5, decimal_places=2, blank=True)

class Erfasst(models.Model):
    id   = models.AutoField(primary_key=True)
    code = models.ForeignKey(Code, db_column='code')


And the following shell session:

lori@keep-surfing:~/Zeiterfassung$ python manage.py shell
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from Zeiterfassung.Lori.models import *

>>> z = Erfasst.objects.get(id=2726498)
>>> z.code.grenzwert
Decimal('10.00')
>>> type(z.code.grenzwert)
<class 'decimal.Decimal'>
>>> z.code.grenzwert > Decimal(0)
True

>>> z = Erfasst.objects.select_related().get(id=2726498)
>>> z.code.grenzwert
10.0
>>> type(z.code.grenzwert)
<type 'float'>
>>> z.code.grenzwert > Decimal(0)
False


(The last line is the one that caused/causes the problems in my app...)

In the second part of the example with select_related(), I'd have expected that it returns type decimal.Decimal as in the first.

Is this a bug in Django?
If not, is there a better solution to the problem than using isinstance() in every place where I have to compare DecimalField values? (I'm still quite new to Python, and might be overlooking something obvious...)

Python 2.7 seems to handle Decimal-float comparisons differently/better than 2.6, and the above would probably silently work with Python 2.7, but I currently cannot upgrade to Python 2.7.

I'd be very grateful for your advice.

Best regards,
Carsten



--
   Cafu - the open-source Game and Graphics Engine
for multiplayer, cross-platform, real-time 3D Action
          Learn more at http://www.cafu.de

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

Reply via email to