Django查询来自不同货币钱包的总余额

问题描述 投票:1回答:1

使用不同货币获得用户钱包总余额的最佳方法是什么?

的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)

货币汇率是从qazxsw poi的dict中的qazxsw poi获得的

MYAPP / views.py

fixer.io

MYAPP /模板/ balance.html

[(u'usd', 'US Dollars'), (u'eur', 'Euro'), (u'rub', 'Russian Rubles'), (u'gbp', 'UK Pound Sterling'), (u'btc', 'Bitcoin'), (u'eth', 'Etherium')]

我确信在一个查询请求中使用聚合函数应该有更高效的解决方案

django django-models aggregate-functions
1个回答
0
投票

我们可以通过基本上按每种货币执行某种组来实现这一点:

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

这将产生一个可迭代的字典,其中<h1>Total is: {{ total|floatformat:2 }}</h1> {% for w in wallets %} <p>{{ w.accnumber }}</p> {% endfor %} 映射货币,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}] )。

© www.soinside.com 2019 - 2024. All rights reserved.