Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django error: [<class 'decimal.InvalidOperation'>]

I have done the following signal in my project:

@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)

My models are:

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)

It was working fine in the beginning but all of a sudden when I am increasing the load of the database by putting datas into fields.

It is throwing me the error [<class 'decimal.InvalidOperation'>].

What this error implies?

Any idea anyone

Thank you

like image 767
Niladry Kar Avatar asked Jun 05 '19 10:06

Niladry Kar


2 Answers

I recently got this problem myself. The way I thought I knew about decimal fields was wrong in my case.

I thought max_digits=x gives x values for the integer part and decimal_places=y gives y values for the decimal part but I was wrong.

max_digits define the total no. of digits overall and decimal places are part of the max_digits.

for eg. if max_digits=13 and decimal_places=3 then the field will store numeric values up to a billion with 3 precise decimals.

This may help someone who got stuck as I did for the past few hours.

like image 186
Anjaan Avatar answered Nov 09 '22 10:11

Anjaan


I had a similar problem and what I have done to solve it is to make sure that the value I want to save fits in the field, two things:

  1. First, use round(myValueFloat, 4). It is, to reduce the number of decimal numbers I try to save.
  2. Use bigger fields for the values I can not reduce.
like image 39
Manuel Gijón Avatar answered Nov 09 '22 11:11

Manuel Gijón