Philip Mutua
Philip Mutua

Reputation: 6891

How to add a choices Field that will list currencies from babel

I came across babel documentation and I would like to use it in a model that has a currencies = models.ChoiceField. How do I use babel inbuilt currencies to list in the above field name?

Upvotes: 0

Views: 349

Answers (1)

mdargacz
mdargacz

Reputation: 1379

If you want to use Babel for that you can try something like this:

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
    )   

Upvotes: 2

Related Questions