Simple Django multicurrency field
Project description
Django Multicurrency
Simple model multicurrency application for Django
A brief overview
Quick start
Installation via pip
pip install django-multicurrency
Add the following settings:
Install multicurrency app
INSTALLED_APPS += (
'parler',
)
Add currencies list and Exchange model to settings
MULTICURRENCY_CURRENCIES = ['UAH', 'EUR', 'USD']
MULTICURRENCY_EXCHANGER = 'shop.models.ExchangeRate'
Creating models
Create Exchange model
Exchange model must contains staticmethod "get_exchange_rate
", which must return exchange rate for two input currencies
from django.db import models
from django.utils.translation import gettext, gettext_lazy as _
from multicurrency.models import CURRENCIES
class ExchangeRate(models.Model):
currency_from = models.CharField(_('Currency'),
max_length=3,
choices=CURRENCIES,
db_index=True,
null=False)
currency_to = models.CharField(_('Currency'),
max_length=3,
choices=CURRENCIES,
db_index=True,
null=False)
exchange_rate = models.FloatField(_('Exchange rate'),
null=False)
created_at = models.DateTimeField(_('Created at'),
blank=True,
null=True,
auto_now_add=True)
@staticmethod
def get_exchange_rate(currency_from:str, currency_to:str):
try:
return ExchangeRate.objects.filter(
currency_from=currency_from.lower(),
currency_to=currency_to.lower()
).latest('created_at').exchange_rate
except ExchangeRate.DoesNotExist:
return None
def __str__(self) -> str:
return f"{self.get_currency_from_display()} -> " + \
f"{self.get_currency_to_display()} {self.exchange_rate}"
Create Price model
from django.db import models
from multicurrency.fields import MultiCurrencyPriceField
from multicurrency.models import MulticurrencyPriceModelMixin
class Product(MulticurrencyPriceModelMixin):
title = models.CharField()
price = MultiCurrencyPriceField(verbose_name="Product price")
MultiCurrencyPriceField accept as kwargs:
- verbose_name
- null
- blank
Accessing fields
MultiCurrencyPriceField has 3 methods to access price fields:
- PriceModel.create_<price_field_title>(price_field_title, currency, amount)
- PriceModel.update_<price_field_title>(price_field_title, currency, amount)
- PriceModel.delete_<price_field_title>(price_field_title)
>>> from shop.models import Product
>>> product = Product.objects.create(title='Jeans')
>>> product.create_price('price','USD',10)
<ProductPrice: EUR(11.7) USD(10)>
>>> product.update_price('price','USD',20)
<ProductPrice: EUR(23.4) USD(20)>
>>> product.delete_price('price')
None
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Close
Hashes for django-multicurrency-0.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 637a69c938c1ecbd6689a17472a81cc908d68ae67e5869b274d1dd3a6e45adae |
|
MD5 | 79382a5c4a5f2aefee4f45578f04549a |
|
BLAKE2b-256 | aed50e9a09182b334e7bb8596eba0ac9a0dcb49bcdfa284bd46f9c4d5223d305 |