[Fixed]-TypeError("Cannot convert %r to Decimal" % value) TypeError: Cannot convert <LenderInvestment: 20000.00> to Decimal

1👍

You have two fields called the same thing, and you are getting confused between them. Your signal is called on LoanDisburs*m*nt, and that model has a OneToOneField called initial_capital. That value can’t be converted to Decimal, since it is a relationship, not a number.

However, the model it has a relationship to, LenderInvestment, also has a field called initial_capital. It is that value that is a number. You need to follow the relationship in your signal, and use that related instance both when getting and setting the value.

def loan_disburs*m*nt_receiver(sender, instance, *args, **kwargs):
    investment = instance.initial_capital
    investment.initial_capital -= money_disbursed

Note a) there is no need to convert these values to Decimal, since they already are; b) there is no need to use intermediate variables; and c) you can use -= to do the subtraction inline.

Also note, depending on your use case, you will probably need to call save on the investment instance.

Finally, note that this would all be a lot less confusing if you called the OneToOneField what it actually is; eg lender_investment.

Leave a comment