Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

quantize result has too many digits for current context

Im trying to save the operation result in my admin.py and i have this error:

quantize result has too many digits for current context

....
def save_model(self, request, obj, form, change):

    usuario_libra   = obj.consignee.membresia.libra
    valores         = Valores.objects.get(pk=1)
    vtasa           = valores.tasa
    vaduana         = valores.aduana
    vgestion        = valores.gestion
    vfee            = valores.fee
    vcombustible    = valores.combustible

    trans_aereo     = obj.peso * usuario_libra * vtasa
    aduana          = (obj.peso * vaduana )*vtasa
    fee_airpot      = (obj.peso * vfee)*vtasa
    combustible     = (obj.peso * vcombustible)*vtasa
    itbis           = (trans_aereo+vgestion)*Decimal(0.16)
    total           = trans_aereo + vgestion + aduana + fee_airpot + combustible + itbis

    if not obj.id:
        obj.total = total
        ...

What this mean?, all my model fields are Decimal Any help please

Thank you

like image 587
Asinox Avatar asked May 18 '12 15:05

Asinox


1 Answers

I was able to solve this problem by increasing the the 'max_digits' field option.

class Myclass(models.Model):
  my_field = models.DecimalField(max_digits=11, decimal_places=2, blank=True, null=True)

Be sure to make it large enough to fit the longest number you wish to save.

If that does not work may also need to set the precision larger by:

from decimal import getcontext
  ...
  getcontext().prec = 11

See the python decimal documentation for full context parameters.

like image 129
dyst-mingus Avatar answered Oct 13 '22 20:10

dyst-mingus