Как лучше всего получить общий баланс пользовательских кошельков с разными валютами?
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 / templates/balance.html
<h1>Total is: {{ total|floatformat:2 }}</h1>
{% for w in wallets %}
<p>{{ w.accnumber }}</p>
{% endfor %}
Я уверен, что должно быть более эффективное решение с использованием функций Aggregate в одном запросе