Search code examples
pythondjangopython-2.7django-1.5django-countries

django countries currency code


I am using django_countries to show the countries list. Now, I have a requirement where I need to show currency according to country. Norway - NOK, Europe & Afrika (besides UK) - EUR, UK - GBP, AMERICAS & ASIA - USDs.

Could this be achieved through django_countries project? or are there any other packages in python or django which I could use for this?

Any other solution is welcomed as well.

--------------------------- UPDATE ------------- The main emphasis is on this after getting lot of solutions: Norway - NOK, Europe & Afrika (besides UK) - EUR, UK - GBP, AMERICAS & ASIA - USDs.

---------------------------- SOLUTION --------------------------------

My solution was quite simple, when I realized that I couldnt get any ISO format or a package to get what I want, I thought to write my own script. It is just a conditional based 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'

Dont know whether this is efficient way or not, would like to hear some suggestions.

Thanks everyone!


Solution

  • There are several modules out there:

    • 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 regularly updated, py-moneyed looks great and has more features than python-money, plus python-money is not maintained now.

    Hope that helps.