Skip to content

Commit

Permalink
Merge pull request #524 from devartis/master
Browse files Browse the repository at this point in the history
Set default decimal_places value to settings configuration and use de…
  • Loading branch information
benjaoming committed Nov 7, 2019
2 parents a8df354 + 784afbd commit a4a47f6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 deletions djmoney/models/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def __init__(
verbose_name=None,
name=None,
max_digits=None,
decimal_places=None,
decimal_places=DECIMAL_PLACES,
default=None,
default_currency=DEFAULT_CURRENCY,
currency_choices=CURRENCY_CHOICES,
Expand Down Expand Up @@ -275,7 +275,7 @@ def get_default(self):
return super().get_default()

def formfield(self, **kwargs):
defaults = {"form_class": forms.MoneyField, "decimal_places": DECIMAL_PLACES}
defaults = {"form_class": forms.MoneyField, "decimal_places": self.decimal_places}
defaults.update(kwargs)
defaults["currency_choices"] = self.currency_choices
defaults["default_currency"] = self.default_currency
Expand Down
14 changes: 12 additions & 2 deletions tests/test_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from .testapp.forms import (
DefaultMoneyModelForm,
DefaultPrecisionModelForm,
DisabledFieldForm,
MoneyForm,
MoneyFormMultipleCurrencies,
Expand Down Expand Up @@ -185,8 +186,17 @@ def test_has_changed(self):
assert not form.has_changed()


@pytest.mark.parametrize("model_class", (PreciseForm, PreciseModelForm))
@pytest.mark.parametrize("model_class", (PreciseForm, DefaultPrecisionModelForm))
def test_decimal_places_model_form(model_class):
"""Forms should use DECIMAL_PLACES setting value."""
"""Forms should use DECIMAL_PLACES setting value when none specified."""

expected = str(10 ** -settings.DECIMAL_PLACES)
assert model_class().fields["money"].widget.widgets[0].attrs["step"] == expected


def test_precedence_decimal_places_model_form():
"""Forms should use decimal_places in field value when specified."""

decimal_places = PreciseModelForm.Meta.model._meta.fields[2].decimal_places
expected = str(10 ** -decimal_places)
assert PreciseModelForm().fields["money"].widget.widgets[0].attrs["step"] == expected
7 changes: 7 additions & 0 deletions tests/testapp/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from .models import (
ModelWithDefaultAsString,
ModelWithDefaultPrecision,
ModelWithValidation,
ModelWithVanillaMoneyField,
NullMoneyFieldModel,
Expand Down Expand Up @@ -49,6 +50,12 @@ class Meta:
fields = ("money",)


class DefaultPrecisionModelForm(forms.ModelForm):
class Meta:
model = ModelWithDefaultPrecision
fields = ("money",)


class MoneyModelFormWithValidation(forms.ModelForm):
class Meta:
model = ModelWithValidation
Expand Down
4 changes: 4 additions & 0 deletions tests/testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,7 @@ class CryptoModel(models.Model):

class PreciseModel(models.Model):
money = MoneyField(max_digits=10, decimal_places=4)


class ModelWithDefaultPrecision(models.Model):
money = MoneyField(max_digits=10)

0 comments on commit a4a47f6

Please sign in to comment.