Skip to main content

Django currencies with historical exchange rates

This is a fairly minimal Django app that renders amounts of currencies, stores historical exchange rates in the database, and performs currency conversion.

When working with any historical multi-currency data, one often needs to be able to perform exchange rate calculations with historical rates. This allows the conversion to be accurately reproduced in the future.

Exchange Rates are stored in a simple database table, with 1 row per rate per date. Conversions can be done directly, or across a base currency using 2 rates.

Formatting is defined by iso4217. Exchange Rate data can be sourced from:

Installation

  1. Install django-historical-currencies in your Python environment.
  2. Add historical_currencies to INSTALLED_APPS in your settings.py.
  3. Import yesterday's exchange rates to get started: manage.py import_ecb_exchangerates --daily.
  4. Configure a periodic task (e.g. cron, systemd timer, celery beat) to import daily exchange rates.

Settings

  • MAX_EXCHANGE_RATE_AGE: How many days old can an exchange rate be treated as current?

Optional settings, only required for OpenExchangeRates.org import:

  • OPEN_EXCHANGE_RATES_APP_ID: OpenExchangeRates App ID.
  • OPEN_EXCHANGE_RATES_BASE_CURRENCY: Base currency.

Usage

In code, amounts can be converted using the historical_currencies.exchange.exchange() method.

In templates, this module represents financial amounts as tuple of (Decimal, str(currency-code)). The recommended approach is to add properties to your Django models to return this tuple for amounts.

In a template the amount can be displayed or converted:

{% load currency_format %}

Assuming my_amount = (Decimal(10), 'USD')
Display:
{{ my_amount|currency }}
-> 10.00 USD

Exchange at the latest rate:
{{ my_amount|exchange:"EUR" }}
-> 9.06 EUR

Currency Selectors:

There are two template tags to help render currency selectors. A high-level one that renders HTML:

{% load currency_choices %}

<select>
  {% currency_choices_options selected="USD" %}
</select>

And a low-level one that returns a list of currencies:

{% load currency_choices %}

<select>
  {% currency_choices_list as currency_choices %}
  {% for code, name in currency_choices %}
    <option value="{{ code }}">{{ name }}</option>
  {% endfor %}
</select>

License

This Django app is available under the terms of the ISC license, see LICENSE.

Metadata

Release files for django-historical-currencies 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-historical-currencies 0.0.3
File Size Uploaded
django_historical_currencies-0.0.3.tar.gz 12.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-historical-currencies 0.0.3
File Interpreter ABI Platform
django_historical_currencies-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 30.0 kB

Release files / django_historical_currencies-0.0.3.tar.gz

Download URL django_historical_currencies-0.0.3.tar.gz
Size 12.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a38dde4154fef1caf26dec0cf54977f070539c5d09773369a0221c8c2e45847a
BLAKE2b-256 checksum
How to use checksums
21206382fa35291be93fe1a72ade724f8d30c47b707958da10eebd927fb84a8c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.3

Release files / django_historical_currencies-0.0.3-py3-none-any.whl

Download URL django_historical_currencies-0.0.3-py3-none-any.whl
Size 18.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1dd5f21e35105edf6b8940db6d16b719b4463a82f7cc959326b9a11a7788e097
BLAKE2b-256 checksum
How to use checksums
ca79c7f0ae26afe9c4499eaa8d30427dcb689c01ec86a7e0c8f5c6c0b3ff83f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page