Reputation: 119
Let us consider my views.py
def ajax_add_other_order_item(request,id):
client = request.user.client
def _convert(from_currency, to_currency, price):
custom_rate_obj = 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)
if request.method == 'POST':
unit = request.POST.get('u_price') or 0
if supplier.currency:
currency = supplier.currency
else:
currency = request.POST.get('currency', 'GBP')
purchase_price = _convert(currency, 'GBP', float(unit))
try:
exchange_price = float(unit)/purchase_price
except ZeroDivisionError:
exchange_price = 0
if form_brought_in == 'CT':
val = 2.5
elif form_brought_in == 'BR' or form_brought_in == 'RB':
val = 3.5
else:
val = 0.0
trade_price = math.ceil(_convert(currency, client.currency, float(unit)) * val)
here is my models.py (where custom_rate_obj.convert function is defined)
class ExchangeRates(TimeStampedModel):
date = models.DateField(unique=True)
timestamp = models.BigIntegerField(blank=True, null=True)
base_currency = models.CharField(max_length=10, default='GBP')
data = JSONField()
def get_currency_rate(self, currency):
return Decimal(self.data[currency])
def convert(self, from_currency, to_currency, value):
from_rate = self.get_currency_rate(from_currency)
to_rate = self.get_currency_rate(to_currency)
return round((Decimal(value or 0) / from_rate) * to_rate, 6)
Here is my error traceback
Traceback (most recent call last):
File "/home/harika/lightdegree/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/harika/lightdegree/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/harika/lightdegree/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/harika/lightdegree/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/harika/lightdegree/lib/python3.7/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/harika/krishna test/dev-1.8/mcam/server/mcam/stock/views.py", line 4476, in ajax_add_other_order_item
trade_price = math.ceil(_convert(currency, client.currency, float(unit)) * val)
TypeError: unsupported operand type(s) for *: 'decimal.Decimal' and 'float'
Here i am getting error TypeError: unsupported operand type(s) for /: 'float' and 'decimal.Decimal' Please help to solve this issue
Upvotes: 2
Views: 6435
Reputation: 476534
You can not divide a float
with a Decimal
, since a decimal has a fixed number of digits, whereas a float
uses the IEEE-754 encoding and has not a fixed number of digits.
You can convert the number to a Decimal
, and then do the division accordingly:
from decimal import Decimal
try:
exchange_price = Decimal(unit)/purchase_price
except ZeroDivisionError:
exchange_price = Decimal(0)
for the val
s you should do the same:
if form_brought_in == 'CT':
val = Decimal('2.5')
elif form_brought_in == 'BR' or form_brought_in == 'RB':
val = Decimal('3.5')
else:
val = Decimal(0)
Upvotes: 4
Reputation: 3700
from decimal import Decimal
...
exchange_price = Decimal(unit)/purchase_price
Upvotes: 2