Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Set decimal places to default currency's fraction

Pacu2adfb92cfe54a
diff --git a/saleor/cart/models.py b/saleor/cart/models.py
index 6096a9d185..5151d2d80b 100644
--- a/saleor/cart/models.py
+++ b/saleor/cart/models.py
@@ -72,8 +72,8 @@ class Cart(models.Model):
on_delete=models.SET_NULL)
checkout_data = JSONField(null=True, editable=False)
total = MoneyField(
- currency=settings.DEFAULT_CURRENCY, max_digits=12, decimal_places=2,
- default=0)
+ currency=settings.DEFAULT_CURRENCY, max_digits=12,
+ decimal_places=settings.DEFAULT_DECIMAL_PLACES, default=0)
quantity = models.PositiveIntegerField(default=0)
objects = CartQueryset.as_manager()
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index d31ef70552..a58f09c65f 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -267,7 +267,7 @@ class ManagePaymentForm(forms.Form):
label=pgettext_lazy(
'Payment management form (capture, refund, release)', 'Amount'),
max_digits=12,
- decimal_places=2,
+ decimal_places=settings.DEFAULT_DECIMAL_PLACES,
currency=settings.DEFAULT_CURRENCY)
def __init__(self, *args, **kwargs):
diff --git a/saleor/discount/models.py b/saleor/discount/models.py
index 490bb6887d..0ac06440fa 100644
--- a/saleor/discount/models.py
+++ b/saleor/discount/models.py
@@ -47,7 +47,8 @@ class Voucher(models.Model):
discount_value_type = models.CharField(
max_length=10, choices=DiscountValueType.CHOICES,
default=DiscountValueType.FIXED)
- discount_value = models.DecimalField(max_digits=12, decimal_places=2)
+ discount_value = models.DecimalField(
+ max_digits=12, decimal_places=settings.DEFAULT_DECIMAL_PLACES)
# not mandatory fields, usage depends on type
product = models.ForeignKey(
@@ -56,8 +57,8 @@ class Voucher(models.Model):
'product.Category', blank=True, null=True, on_delete=models.CASCADE)
apply_to = models.CharField(max_length=20, blank=True, null=True)
limit = MoneyField(
- currency=settings.DEFAULT_CURRENCY, max_digits=12, decimal_places=2,
- null=True, blank=True)
+ currency=settings.DEFAULT_CURRENCY, max_digits=12,
+ decimal_places=settings.DEFAULT_DECIMAL_PLACES, null=True, blank=True)
objects = VoucherQueryset.as_manager()
@@ -140,7 +141,9 @@ class Sale(models.Model):
type = models.CharField(
max_length=10, choices=DiscountValueType.CHOICES,
default=DiscountValueType.FIXED)
- value = models.DecimalField(max_digits=12, decimal_places=2, default=0)
+ value = models.DecimalField(
+ max_digits=12, decimal_places=settings.DEFAULT_DECIMAL_PLACES,
+ default=0)
products = models.ManyToManyField('product.Product', blank=True)
categories = models.ManyToManyField('product.Category', blank=True)
diff --git a/saleor/order/models.py b/saleor/order/models.py
index 2b6ac69a20..23f2f8cc6f 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -60,10 +60,12 @@ class Order(models.Model):
ShippingMethodCountry, blank=True, null=True, related_name='orders',
on_delete=models.SET_NULL)
shipping_price_net = MoneyField(
- currency=settings.DEFAULT_CURRENCY, max_digits=12, decimal_places=2,
+ currency=settings.DEFAULT_CURRENCY, max_digits=12,
+ decimal_places=settings.DEFAULT_DECIMAL_PLACES,
default=0, editable=False)
shipping_price_gross = MoneyField(
- currency=settings.DEFAULT_CURRENCY, max_digits=12, decimal_places=2,
+ currency=settings.DEFAULT_CURRENCY, max_digits=12,
+ decimal_places=settings.DEFAULT_DECIMAL_PLACES,
default=0, editable=False)
shipping_price = TaxedMoneyField(
net_field='shipping_price_net', gross_field='shipping_price_gross')
@@ -71,17 +73,17 @@ class Order(models.Model):
max_length=255, null=True, default=None, blank=True, editable=False)
token = models.CharField(max_length=36, unique=True)
total_net = MoneyField(
- currency=settings.DEFAULT_CURRENCY, max_digits=12, decimal_places=2,
- default=0)
+ currency=settings.DEFAULT_CURRENCY, max_digits=12,
+ decimal_places=settings.DEFAULT_DECIMAL_PLACES, default=0)
total_gross = MoneyField(
- currency=settings.DEFAULT_CURRENCY, max_digits=12, decimal_places=2,
- default=0)
+ currency=settings.DEFAULT_CURRENCY, max_digits=12,
+ decimal_places=settings.DEFAULT_DECIMAL_PLACES, default=0)
total = TaxedMoneyField(net_field='total_net', gross_field='total_gross')
voucher = models.ForeignKey(
Voucher, null=True, related_name='+', on_delete=models.SET_NULL)
discount_amount = MoneyField(
- currency=settings.DEFAULT_CURRENCY, max_digits=12, decimal_places=2,
- default=0)
+ currency=settings.DEFAULT_CURRENCY, max_digits=12,
+ decimal_places=settings.DEFAULT_DECIMAL_PLACES, default=0)
discount_name = models.CharField(max_length=255, default='', blank=True)
objects = OrderQueryset.as_manager()
@@ -188,9 +190,11 @@ class OrderLine(models.Model):
quantity_fulfilled = models.IntegerField(
validators=[MinValueValidator(0), MaxValueValidator(999)], default=0)
unit_price_net = MoneyField(
- currency=settings.DEFAULT_CURRENCY, max_digits=12, decimal_places=2)
+ currency=settings.DEFAULT_CURRENCY, max_digits=12,
+ decimal_places=settings.DEFAULT_DECIMAL_PLACES)
unit_price_gross = MoneyField(
- currency=settings.DEFAULT_CURRENCY, max_digits=12, decimal_places=2)
+ currency=settings.DEFAULT_CURRENCY, max_digits=12,
+ decimal_places=settings.DEFAULT_DECIMAL_PLACES)
unit_price = TaxedMoneyField(
net_field='unit_price_net', gross_field='unit_price_gross')
diff --git a/saleor/product/models.py b/saleor/product/models.py
index 134e6070e0..5a397a1691 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -97,7 +97,8 @@ class Product(SeoModel):
category = models.ForeignKey(
Category, related_name='products', on_delete=models.CASCADE)
price = MoneyField(
- currency=settings.DEFAULT_CURRENCY, max_digits=12, decimal_places=2)
+ currency=settings.DEFAULT_CURRENCY, max_digits=12,
+ decimal_places=settings.DEFAULT_DECIMAL_PLACES)
available_on = models.DateField(blank=True, null=True)
is_published = models.BooleanField(default=True)
attributes = HStoreField(default={}, blank=True)
@@ -180,8 +181,8 @@ class ProductVariant(models.Model):
sku = models.CharField(max_length=32, unique=True)
name = models.CharField(max_length=255, blank=True)
price_override = MoneyField(
- currency=settings.DEFAULT_CURRENCY, max_digits=12, decimal_places=2,
- blank=True, null=True)
+ currency=settings.DEFAULT_CURRENCY, max_digits=12,
+ decimal_places=settings.DEFAULT_DECIMAL_PLACES, blank=True, null=True)
product = models.ForeignKey(
Product, related_name='variants', on_delete=models.CASCADE)
attributes = HStoreField(default={}, blank=True)
@@ -191,8 +192,8 @@ class ProductVariant(models.Model):
quantity_allocated = models.IntegerField(
validators=[MinValueValidator(0)], default=Decimal(0))
cost_price = MoneyField(
- currency=settings.DEFAULT_CURRENCY, max_digits=12, decimal_places=2,
- blank=True, null=True)
+ currency=settings.DEFAULT_CURRENCY, max_digits=12,
+ decimal_places=settings.DEFAULT_DECIMAL_PLACES, blank=True, null=True)
class Meta:
app_label = 'product'
diff --git a/saleor/settings.py b/saleor/settings.py
index 802559576e..9984124a94 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -6,6 +6,7 @@ import dj_email_url
import django_cache_url
from django.contrib.messages import constants as messages
from django.utils.translation import gettext_lazy as _
+from django_prices.templatetags.prices_i18n import get_currency_fraction
def get_list(text):
@@ -272,6 +273,7 @@ LOGIN_URL = '/account/login/'
DEFAULT_COUNTRY = 'US'
DEFAULT_CURRENCY = 'USD'
+DEFAULT_DECIMAL_PLACES = get_currency_fraction(DEFAULT_CURRENCY)
AVAILABLE_CURRENCIES = [DEFAULT_CURRENCY]
OPENEXCHANGERATES_API_KEY = os.environ.get('OPENEXCHANGERATES_API_KEY')
diff --git a/saleor/shipping/models.py b/saleor/shipping/models.py
index a4d7f18929..01596564e7 100644
--- a/saleor/shipping/models.py
+++ b/saleor/shipping/models.py
@@ -81,7 +81,8 @@ class ShippingMethodCountry(models.Model):
choices=COUNTRY_CODE_CHOICES, max_length=2, blank=True,
default=ANY_COUNTRY)
price = MoneyField(
- currency=settings.DEFAULT_CURRENCY, max_digits=12, decimal_places=2)
+ currency=settings.DEFAULT_CURRENCY, max_digits=12,
+ decimal_places=settings.DEFAULT_DECIMAL_PLACES)
shipping_method = models.ForeignKey(
ShippingMethod, related_name='price_per_country',
on_delete=models.CASCADE)