Django 错误:[<class 'decimal.InvalidOperation'>]

Nil*_*Kar 7 django django-signals django-models

我在我的项目中做了以下信号:

@receiver(pre_save, sender=group1)
@disable_for_loaddata
def total_closing_group1(sender,instance,*args,**kwargs):
    total_group_closing_deb_po = instance.master_group.filter(ledgergroups__Closing_balance__gte=0,balance_nature='Debit').aggregate(the_sum=Coalesce(Sum('ledgergroups__Closing_balance'), Value(0)))['the_sum']
    total_group_closing_deb_neg = instance.master_group.filter(ledgergroups__Closing_balance__lt=0,balance_nature='Debit').aggregate(the_sum=Coalesce(Sum('ledgergroups__Closing_balance'), Value(0)))['the_sum']
    total_group_closing_po_cre = instance.master_group.filter(ledgergroups__Closing_balance__gte=0,balance_nature='Credit').aggregate(the_sum=Coalesce(Sum('ledgergroups__Closing_balance'), Value(0)))['the_sum']
    total_group_closing_neg_cre = instance.master_group.filter(ledgergroups__Closing_balance__lt=0,balance_nature='Credit').aggregate(the_sum=Coalesce(Sum('ledgergroups__Closing_balance'), Value(0)))['the_sum']
    total_closing_deb_po = instance.ledgergroups.filter(Closing_balance__gte=0,group1_Name__balance_nature='Debit').aggregate(the_sum=Coalesce(Sum('Closing_balance'), Value(0)))['the_sum']
    total_closing_deb_ne = instance.ledgergroups.filter(Closing_balance__lt=0,group1_Name__balance_nature='Debit').aggregate(the_sum=Coalesce(Sum('Closing_balance'), Value(0)))['the_sum']
    total_closing_cre_po = instance.ledgergroups.filter(Closing_balance__gte=0,group1_Name__balance_nature='Credit').aggregate(the_sum=Coalesce(Sum('Closing_balance'), Value(0)))['the_sum']
    total_closing_cre_ne = instance.ledgergroups.filter(Closing_balance__lt=0,group1_Name__balance_nature='Credit').aggregate(the_sum=Coalesce(Sum('Closing_balance'), Value(0)))['the_sum']
    if total_group_closing_deb_po != None and total_group_closing_neg_cre != None and total_closing_deb_po != None and total_closing_cre_ne != None:
        instance.positive_closing = total_group_closing_deb_po + abs(total_group_closing_neg_cre) + total_closing_deb_po + abs(total_closing_cre_ne)
    if total_group_closing_po_cre != None and total_group_closing_deb_neg != None and total_closing_cre_po != None and total_closing_deb_ne != None:    
        instance.negative_closing = total_group_closing_po_cre + abs(total_group_closing_deb_neg) + total_closing_cre_po + abs(total_closing_deb_ne)
Run Code Online (Sandbox Code Playgroud)

我的模型是:

class Group1(models.Model):   
    group_name = models.CharField(max_length=32)    
    master = models.ForeignKey("self",on_delete=models.CASCADE,related_name='master_group',null=True)    
    negative_closing = models.DecimalField(max_digits=10,default=0,decimal_places=2,null=True)    
    positive_closing = models.DecimalField(max_digits=10,default=0,decimal_places=2,null=True)


class Ledger1(models.Model):
    name            = models.CharField(max_length=32)
    group1_name     = models.ForeignKey(Group1,on_delete=models.CASCADE,null=True,related_name='ledgergroups')
    closing_balance = models.DecimalField(default=0.00,max_digits=10,decimal_places=2,blank=True)
Run Code Online (Sandbox Code Playgroud)

一开始它工作正常,但是当我通过将数据放入字段来增加数据库的负载时突然之间。

它向我抛出错误[<class 'decimal.InvalidOperation'>]

这个错误意味着什么?

任何想法任何人

谢谢

Anj*_*aan 7

我最近自己也遇到了这个问题。我认为我知道十进制字段的方式在我的情况下是错误的。

我认为 max_digits=x 给出整数部分的 x 值,decimal_places=y 给出小数部分的 y 值,但我错了。

max_digits 定义总数。总位数和小数位数是 max_digits 的一部分。

例如。if max_digits=13and decimal_places=3then 该字段将存储多达 10 亿的数值,带有 3 个精确的小数。

这可能会帮助那些像我过去几个小时一样陷入困境的人。


小智 5

我遇到了类似的问题,我所做的解决方法是确保我想要保存的值适合该字段,有两件事:

  1. 首先,使用round(myValueFloat, 4). 是的,为了减少我尝试保存的小数位数。
  2. 对于我无法减少的值,请使用更大的字段。