如何添加选项将列出来自babel的货币的字段

时间:2018-03-28 10:52:31

标签: django python-3.x django-rest-framework

我遇到babel documentation,我想在具有currencies = models.ChoiceField的模型中使用它。如何使用babel内置货币列出上述字段名称?

1 个答案:

答案 0 :(得分:1)

如果您想使用Babel,可以尝试这样的事情:

from babel.numbers import list_currencies
CURRENCY_CHOICES = [(currency, currency) for currency in list_currencies()] 
# `choices` has to be an iterable (e.g., a list or tuple) consisting
# itself of iterables of exactly two items from which first of values is
# stored in database and the second is for representation.
class ModelName(models.Model):
    currency = models.CharField(
        max_length=3, null=True, blank=True, choices=CURRENCY_CHOICES
    )
    currency_with_default = models.CharField(
        max_length=3, default='USD', choices=CURRENCY_CHOICES
    )