Currency code of django countries

I use django_countries to show a list of countries. Now I have a requirement when I need to show the currency depending on the country. Norway - NOK, Europe and Africa (except Great Britain) - EUR, UK - GBP, AMERICAS and ASIA - USD.

Can this be achieved through the django_countries project? or are there other packages in python or django that I could use for this?

Any other solution is also welcome.

--------------------------- UPDATE ------------- The main emphasis is on this after receiving many decisions : Norway - NOK, Europe & Afrika (besides UK) - EUR, UK - GBP, AMERICAS & ASIA - USDs.

---------------------------- DECISION ------------------ --- -----------

My solution was pretty simple when I realized that I couldn’t get any ISO format or package to get what I want, I thought to write my own script. This is just conditional logic:

 from incf.countryutils import transformations def getCurrencyCode(self, countryCode): continent = transformations.cca_to_ctn(countryCode) # print continent if str(countryCode) == 'NO': return 'NOK' if str(countryCode) == 'GB': return 'GBP' if (continent == 'Europe') or (continent == 'Africa'): return 'EUR' return 'USD' 

I do not know if it is effective or not, I would like to hear some suggestions.

Thanks everyone!

+7
python django django-countries
source share
3 answers

There are several modules:

  • pycountry :

     import pycountry country = pycountry.countries.get(name='Norway') currency = pycountry.currencies.get(numeric=country.numeric) print currency.alpha_3 print currency.name 

    prints:

     NOK Norwegian Krone 
  • py-moneyed

     import moneyed country_name = 'France' for currency, data in moneyed.CURRENCIES.iteritems(): if country_name.upper() in data.countries: print currency break 

    prints EUR

  • python-money

     import money country_name = 'France' for currency, data in money.CURRENCY.iteritems(): if country_name.upper() in data.countries: print currency break 

    prints EUR

pycountry is updated regularly, py-moneyed looks great and has more features than python-money , plus python-money is no longer supported.

Hope this helps.

+10
source share

django-countries just gives you a field to connect to your model (and a static package with flag icons). The field can contain 2 ISO characters from the list in countries.py , which is convenient if this list is updated (not verified), since it saves a lot of input.

If you want to create a model with details that are easy to achieve, for example

 class Country(models.Model): iso = CountryField() currency = # m2m, fk, char or int field with pre-defined # choices or whatever suits you >> obj = Country.objects.create(iso='NZ', currency='NZD') >> obj.iso.code u'NZ' >> obj.get_iso_display() u'New Zealand' >> obj.currency u'NZD' 

An example script for preloading data that can later be exported to create an instrument that is a more convenient way to manage data samples.

 from django_countries.countries import COUNTRIES for key in dict(COUNTRIES).keys(): Country.objects.create(iso=key) 
+2
source share

I just released country-currencies , a module that gives you the display of country codes in currencies.

 >>> from country_currencies import get_by_country >>> get_by_country('US') ('USD',) >>> get_by_country('ZW') ('USD', 'ZAR', 'BWP', 'GBP', 'EUR') 
+1
source share

All Articles