通貨を変換する関数を使用しようとしています。しかし、私は間違いを続けている。クエリーセットで関数にエラーが発生しました
タイプエラー - 文字列のインデックスは
誰でも助けることができますがstrがない、整数でなければなりませんか?
トレースバック(here)
def exchange(price, current, target):
try:
price *= Exchangerate.objects.get(basecurrency=current, targetcurrency=target).exchangerate
except Exception:
pass
return price
def symbol(symbol):
try:
symbol = Currency.objects.get(symbol=symbol).symbol
except Exception:
print
return symbol
..............
class ProductItemView(APIView):
renderer_classes = (JSONRenderer,)
def get(self, request, *args, **kwargs):
try:
pk = int(kwargs['pk'])
except Exception:
return HttpResponse(status=status.HTTP_400_BAD_REQUEST)
result = (
Product.objects
.filter(pk=pk)
.values('pk', 'name',)
.annotate(
currency=F('variation__price__currency'),
currencycode=F('variation__price__currency__currencycode'),
symbol=F('variation__price__currency__symbol'),
price=F('variation__price__price'),
)
.order_by('variation__price__created')
).first()
img = Image.objects.filter(variation__product=pk).all().values('image')
result['image'] = list(set(i["image"] for i in img))
for i in result:
i['price'] = str(i['price']) + ' (Estimate: ' + (symbol(user.settings_currency.symbol)) + ' ' + "{:.2f}".format((exchange(i['price'], i['currency'], user.settings_currency.id,))) + ')'
del i['price']
return Response(result)
結果に何が印刷できますか?ほとんどの場合、インデックスとして '価格'のリストをループすることはできません。 – lapinkoira
プリントはどこに貼り付け、どのように書きますか? – Yian
あなたのDjangoはどうやって動いていますか? – lapinkoira