Home > OS >  unsupported operand type(s) for =: 'int' and 'Decimal128'
unsupported operand type(s) for =: 'int' and 'Decimal128'

Time:12-25

Im trying to add the numbers from Mongodb database.

#code

meal_data = Data.objects.filter(year= se_year, month= se_month, category = 'Meal')
    meal_amt = 0
    for i in meal_data:
        id = i.id
        m_amount_data = Data.objects.get(id = id)
        meal_amt  = m_amount_data.amount

#error

TypeError: unsupported operand type(s) for =: 'int' and 'Decimal128'

the error is showing in this line

        meal_amt  = m_amount_data.amount

I need to add those numbers and store it to a variable "meal_amt".

CodePudding user response:

change the type of meal_amt variable into Decimal.

from decimal import Decimal

meal_data = Data.objects.filter(year= se_year, month= se_month, category = 'Meal')
meal_amt = Decimal(0)

for i in meal_data:
    meal_amt  = i.amount

Also, no need to fetch the Data object in for loop.

CodePudding user response:

you need to convert it to int or float to use =

meal_amt  = int(m_amount_data.amount)
meal_amt  = float(m_amount_data.amount)

in order to use the operator =

  • Related