¿Puedo recuperar un decimal de Avg en un DecimalField en Django?

Cuando se utiliza el Avg aggregate for a models.DecimalField, the returned value is a float, en vez de una decimal.Decimal. Is this because my specific database (in this case sqlite3) doesn't support aggregate values as decimals? If so, are there databases that do? Is there a way around this (ie, a way to get the value back as a decimal), so that accurate decimal math can be done (also, without converting the float a una str y luego de vuelta a un decimal.Decimal)?

Note: I'm not trolling for fights with people who believe that "accurate enough" is the same as "accurate", or flame baiting for discussions about the Star Ship Enterprise crashing due to erroneous, float-based arithmetic.

preguntado el 01 de febrero de 12 a las 03:02

Yo solo busqué en google star ship enterprise float based arithmetic. Anyways, on PostgreSQL I get a float as well. -

That's an oddly specific note. -

1 Respuestas

Desde el SQLLite 3 manual, supported data types are null, integer, real (efectivamente float), texty blob. Based on this I'd expect the engine to cast any input decimal.Decimal en un real then return it as a float to you, since it wouldn't want to upcast it past it's accuracy.

Most actual databases (I'd hardly consider sqllite for anything more than a prototype) support decimal data types. I know MySQL hace. As to whether or not they return decimal.Decimal más bien que float, I couldn't tell you unfortunately as I haven't done the investigation into their accuracy, float has been good enough for me. It'd be a good place to start though.

Respondido 01 Feb 12, 21:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.