select_related() changes type of DecimalField?

9 views
Skip to first unread message

Carsten Fuchs

unread,
Apr 4, 2011, 12:05:14 PM4/4/11
to django...@googlegroups.com
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

Ian

unread,
Apr 4, 2011, 4:27:58 PM4/4/11
to Django users
On Apr 4, 12:05 pm, Carsten Fuchs <CarstenFu...@T-Online.de> wrote:
> 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...)

Yes, this appears to be a bug. If you would, please create a ticket
for it in the Django Trac so that we don't forget about it.

Thanks,
Ian

Carsten Fuchs

unread,
Apr 5, 2011, 8:09:01 AM4/5/11
to django...@googlegroups.com
Dear Ian,

Am 04.04.2011 22:27, schrieb Ian:
> Yes, this appears to be a bug. If you would, please create a ticket
> for it in the Django Trac so that we don't forget about it.

Gladly:
http://code.djangoproject.com/ticket/15766

Reply all
Reply to author
Forward
0 new messages