量化结果对于当前上下文具有太多数字

Asi*_*nox 15 django django-models django-admin

我试图在我的admin.py中保存操作结果,我有这个错误:

量化结果对于当前上下文具有太多数字

....
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
        ...
Run Code Online (Sandbox Code Playgroud)

这意味着什么?,我所有的模型字段都是十进制的

谢谢

小智 25

我能够通过增加'max_digits'字段选项来解决这个问题.

class Myclass(models.Model):
  my_field = models.DecimalField(max_digits=11, decimal_places=2, blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)

务必使其足够大,以适合您想要保存的最长数字.

如果不起作用,可能还需要将精度设置为:

from decimal import getcontext
  ...
  getcontext().prec = 11
Run Code Online (Sandbox Code Playgroud)

有关完整上下文参数,请参阅python十进制文档.

  • 请注意,这并不理想,因为它会在任何地方更改上下文,这可能会破坏使用超过11位数的其他代码. (2认同)