apardes
apardes

Reputation: 4380

Django unsupported operand type for instancemethod and decimal

I have a model function that calls another model function and performs a computation with the result. The result returned should be a decimal however I'm getting an error:

unsupported operand type(s) for -: 'instancemethod' and 'Decimal'

I've tried Decimal() to convert the return but that resulted in the same error but saying that I couldn't convert instancemethod to decimal.

The relevant code is:

cimt = member.cimt

bal = cimt - cpp

if bal == 0:
    pass
elif bal < 0:
    owes_members.append(member)
elif bal > 0:
    owed_members.append(member)
    record_item = ReceiptItem(member = member, amount = bal)
    record_item.save()
    print record_item.amount

The line where the error occurs is bal = cimt - cpp

Any help would be really appreciated, thanks.

Upvotes: 0

Views: 373

Answers (1)

Peter DeGlopper
Peter DeGlopper

Reputation: 37319

As the error tells you you're trying to perform a computation with the model method, not the result of calling the method. It should be:

cimt = member.cimt()

Or called with whatever arguments you need.

You can access and manipulate methods by name just like anything else on an object; you need to use the parentheses to actually call it.

Upvotes: 1

Related Questions