【发布时间】:2022-01-03 10:53:57
【问题描述】:
让我们考虑一下我的观点.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)
这是我的 models.py(其中定义了 custom_rate_obj.convert 函数)
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)
这是我的错误回溯
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'
这里我收到错误 TypeError: unsupported operand type(s) for /: 'float' and 'decimal.Decimal' 请帮助解决这个问题
【问题讨论】:
标签: python python-3.x django django-models django-views