首页 > 解决方案 > 我如何在 settings.py 文件中设置 default_currency= "INR" 因为它在项目中多次使用?

问题描述

我想将 MoneyField 详细信息保存在 setting.py 文件中,这样我就可以在每个使用 MoneyField 的地方使用它我想保存它..

MoneyField(max_digits=10, decimal_places=2, default_currency='INR', null=False, default=0.0)

如何将其保存在settings.py中

标签: djangomodelsettingscurrencymoney-format

解决方案


Django uses a setting named DEFAULT_CURRENCY to set the currency, the default is 'XYZ'. Indeed, if we look at the source code [GitHub], we see:

from ..settings import CURRENCY_CHOICES, DECIMAL_PLACES, DEFAULT_CURRENCY

# …

class MoneyField(models.DecimalField):
    # …

    def __init__(
        self,
        # ...,
        default_currency=DEFAULT_CURRENCY,
        # …
    ):
        # …
    
    # …

You thus can set the DEFAULT_CURRENCY in the settings.py to:

# settings.py

# …

DEFAULT_CURRENCY = 'INR'

# …

and the omit the default_currency='INR' parameters when you create MoneyFields.


推荐阅读