Неподдерживаемый тип(ы) операнда для -: 'decimal.Decimal' и 'float' в Python 3

Я получаю эту проблему неподдерживаемого типа(ов) операнда для -: 'decimal.Decimal' и 'float'

Вот мой models.py

def _convert(self,from_currency, to_currency, price):
        custom_rate_obj = self.quote.client.custom_rates.filter(currency=to_currency).first()
        if custom_rate_obj is None or custom_rate_obj.exchange_rate in (0, None):
            custom_rate_obj = ExchangeRates.objects.latest('created')
        return custom_rate_obj.convert(from_currency, to_currency, price)

    def get_margin(self):
        if self.cost_price:
            cost_price = self.cost_price
            unit_price = self._convert(self.currency, self.cost_currency, self.unit_price)
            if unit_price:
                return ((unit_price - float(cost_price))/unit_price) * 100

Как я могу решить свою проблему

Замените unit_price на float также

 return ((float(unit_price) - float(cost_price))/unit_price) * 100

И всегда используйте типизацию в python 3, чтобы избежать таких ошибок.

def _convert(self,from_currency: str, to_currency : str, price: float) => float:
Вернуться на верх