views:

279

answers:

2

I am trying to put infinity in a FloatField, but that doesn't seem to work. How do I solve this?

f = DjangoModel(float_value=float('inf')) #ok
f.save() #crashes

Results in:

Traceback (most recent call last):
...
ProgrammingError: column "inf" does not exist
LINE 1: ... "float_value") VALUES (inf)

I'm using Django 1.0.2 with PostgreSQL 8.3

A: 

It should be 'infinity' shouldn't it? However, I don't know if that can be stored in a float field. float('infinity') returns inf, and that would be a char field.

Edit: Unfortunately, it is hard to say what C library Django uses without digging deep. I'd say figure out what the value stored in the float field actually is, or at least determine what is being returned. If it is indeed "inf", then that cannot be stored properly in a float field.

AlbertoPL
float('inf') and float('infinity') are the same, but it is not the string 'inf' or 'infinity'...
Jack Ha
Take a look at this: http://code.djangoproject.com/ticket/4287 It is two years old, but I'm guessing then that it is a problem with PostgreSQL itself.
AlbertoPL
Ok, try seeing if a DecimalField handles it properly.
AlbertoPL
+2  A: 

It seems like Djangos ORM doesn't have any special handling for this. Pythons representaton of the values are inf and -inf, while PostgrSQL wants 'Infinity' and '-Infinity'. Obviously Djangos ORM doesn't handle that conversion.

So you need to fix the ORM, I guess. And then think about the fact that other SQL databases may very well want other formats, or won't handle infinities at all.

Lennart Regebro
I think I'll use 1e100 as infinity... "close" enough, right? :)
Jack Ha
That should work fine. :)
Lennart Regebro