Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Currencies not restricted when CURRENCIES is set in settings.py #683

Open
simensol opened this issue Jul 27, 2022 · 1 comment
Open

Currencies not restricted when CURRENCIES is set in settings.py #683

simensol opened this issue Jul 27, 2022 · 1 comment

Comments

@simensol
Copy link

As the documentation states, one can restrict the currencies listed on the project by setting the CURRENCIES variable with a list of currency codes on settings.py. I have added the following line to settings.py:

CURRENCIES = ["NOK"]

However, when saving an object with another currency than NOK (e.g., USD), no ValidationError or other exception is raised. Thus, I have added a custom check in my model's clean_fields() method:

class Claim(models.Model):
    amount = MoneyField(
        max_digits=12,
        decimal_places=4,
        default_currency="NOK",
        validators=[
            MinMoneyValidator(0),
            MaxMoneyValidator(99999999),
        ],
    )

    objects: ClaimManager = money_manager(ClaimManager())

    def clean_fields(self, *args: Any, **kwargs: Any):
        errors = {}

        # Amount currency
        if not self.amount_currency in settings.CURRENCIES:
            currency_string = ", ".join(settings.CURRENCIES)

            errors["amount_currency"] = [
                _(f"Currency has to be one of {currency_string}")
            ]

        if len(errors) > 0:
            raise ValidationError(errors)

        # Check model validators:
        super().clean_fields(*args, **kwargs)

Is this expected behavior? If so, what then does the documentation mean by one can restrict the currencies listed on the project?

@hosamhamdy258
Copy link
Contributor

same with DRF it's creating objects with any Currency even it's restricted in settings.py ,
any update here it's from Jul 27, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants