Fix convert_prices storing exchange rate as string
floatformat() returns a str; saving that to ExchangeRate.rate (FloatField) via create() leaves the Python instance attribute as a str. Reading it back on the same instance (rate = exchange_rate.rate) then caused `purchase.price * rate` to fail with "can't multiply sequence by non-int of type 'float'". Fix: pass the raw float from the API directly to ExchangeRate.objects.create. Also replace floatformat(..., 0) on the converted price with round(..., 2) to keep a numeric type throughout. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+2
-4
@@ -2,8 +2,6 @@ import logging
|
||||
|
||||
import requests
|
||||
from django.db import models
|
||||
from django.template.defaultfilters import floatformat
|
||||
|
||||
from games.models import ExchangeRate, Purchase
|
||||
|
||||
logger = logging.getLogger("games")
|
||||
@@ -38,7 +36,7 @@ def _get_exchange_rate(currency_from, currency_to, year):
|
||||
currency_from=currency_from,
|
||||
currency_to=currency_to,
|
||||
year=year,
|
||||
rate=floatformat(rate, 2),
|
||||
rate=rate,
|
||||
)
|
||||
rate = exchange_rate.rate
|
||||
else:
|
||||
@@ -84,7 +82,7 @@ def convert_prices():
|
||||
if rate:
|
||||
_save_converted_price(
|
||||
purchase,
|
||||
floatformat(purchase.price * rate, 0),
|
||||
round(purchase.price * rate, 2),
|
||||
needs_update,
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user