我有这个代码:
def getExchangeRates():
""" Here we have the function that will retrieve the latest rates from fixer.io """
rates = []
response = urlopen('http://data.fixer.io/api/latest?access_key=c2f5070ad78b0748111281f6475c0bdd')
data = response.read()
rdata = json.loads(data.decode(), parse_float=float)
rates_from_rdata = rdata.get('rates', {})
for rate_symbol in ['USD', 'GBP', 'HKD', 'AUD', 'JPY', 'SEK', 'NOK']:
try:
rates.append(rates_from_rdata[rate_symbol])
except KeyError:
logging.warning('rate for {} not found in rdata'.format(rate_symbol))
pass
return rates
@require_http_methods(['GET', 'POST'])
def index(request):
rates = getExchangeRates()
return render(request, 'index.html')
结果jsonfromdata.fixer.io具有格式,例如,currency | rate_of_the_currency。
像这样:"rates": {"SAR": 4.394498, "INR": 49.836962, and so on...,所以,我在 Django 上创建了这个模型:
class Fixerio_rates(models.Model):
currency = models.CharField(max_length=128)
rate = models.FloatField()
现在,如何将代码的结果保存到此模型中?迁移已经完成,它不应该是一件复杂的事情,但是由于这是从迁移Flask到Django它让我有点困惑。这是一种不同的方法,Django 有自己的方法来处理这些事情。
有任何想法吗?
米脂
相关分类