使用不同货币获得用户钱包总余额的最佳方法是什么?
的myapp / models.py
from django.db import models
from django.contrib.auth.models import User
USD = 'USD'
EUR = 'EUR'
GBP = 'GBP'
CURRENCY_CHOICES = (
(USD, 'US Dollars'),
(EUR, 'Euro'),
(GBP, 'UK Pounds'),
)
class Wallet(models.Model):
user = models.ForeignKey(User)
accnumber = models.CharField(max_length=12)
currency = models.CharField(choices=CURRENCY_CHOICES, default=EUR)
balance = models.DecimalField(max_digits=9,decimal_places=2)
货币汇率是以字典形式从fixer.io获得的
[(u'usd', 'US Dollars'), (u'eur', 'Euro'), (u'rub', 'Russian Rubles'), (u'gbp', 'UK Pound Sterling'), (u'btc', 'Bitcoin'), (u'eth', 'Etherium')]
的myapp / views.py
import requests
from decimal import Decimal
from django.conf import settings
from django.views.generic.base import TemplateView
from django.db.models import Sum
from myyapp.model import Wallet, CURRENCY_CHOICES
class TotalBalanceView(TemplateView):
template_name = 'balance.html'
def get_context_data(self, **kwargs):
context = super(TotalBalanceView, self).get_context_data(**kwargs)
#get current exchage rates from Fixer.IO
symbols = ','.join(dict(CURRENCY_CHOICES).keys()).upper()
uri = "http://data.fixer.io/api/latest?access_key={}&base=EUR&symbols={}".format(FIXER_API, symbols)
r = requests.get(uri)
rates = r.json()['rates']
#get account for the user
wallets = Wallet.objects.filter(user=self.request.user)
total = Decimal()
for curr in CURRENCY_CHOICES:
total += wallets.filter(currency=curr).aggregate(
total=Sum('balance'))
context.update({
'wallets ': wallets
'total': total_eur + total_usd * rates['USD'] + total_gbp * rates['GBP']
})
return context
的myapp /模板/ balance.html
<h1>Total is: {{ total|floatformat:2 }}</h1>
{% for w in wallets %}
<p>{{ w.accnumber }}</p>
{% endfor %}
我确信在一个查询请求中使用聚合函数应该有更高效的解决方案
答案 0 :(得分:0)
我们可以通过基本上按每种货币执行某种组来实现这一点:
totals = (wallets.values('currency')
.annotate(total=Sum('balance'))
.order_by('currency'))
这将导致字典的可迭代,其中currency
映射到货币,total
映射到该货币的总和。例如:
[{'currency': 'EUR', 'total': 123.45},
{'currency': 'USD', 'total': 456.78},
{'currency': 'BFR', 'total': 901.23}]
然后我们可以计算一种货币的总数:
total = sum([subsum[total] * rate[subsum['currency']]
for subsum in totals])
您可能需要为转换后的货币添加费率(费率等于1.00
)。