9

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!

Maverick
  • 2,738
  • 24
  • 91
  • 157

3 Answers3

15

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.

Renjith Thankachan
  • 4,178
  • 1
  • 30
  • 47
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thanks, looks promising, but would that give me currency code according to group as well? Like Europe and Afrika (besides UK) it should be EUR and so on? – Maverick Jul 31 '13 at 10:11
  • You are welcome. Mm, I don't think there is one particular module you need. `pycountry` operates on country (and country subdivisions too) level. – alecxe Jul 31 '13 at 10:16
  • Well..here is the thing, I tried your code, it works fine with Norway..but when I replaced it with France, it gave me an error: `u'250'` `site-packages\pycountry\db.py in get, line 87`. In any case it doesnt work like the way I expected, as I tried it with India now, but instead of USD its giving INR. – Maverick Jul 31 '13 at 10:24
  • Yeah, this is because France has no country-specific currency. `French Franc` was withdrawn in 2002. FYI, `pycountry` uses [ISO 4217](http://en.wikipedia.org/wiki/ISO_4217) standard for currencies.. – alecxe Jul 31 '13 at 10:36
  • How to define that the currency for France is `EUR` - this is a good and open question. – alecxe Jul 31 '13 at 10:37
  • @mad_programmer I've added 2 more options, please check. – alecxe Jul 31 '13 at 10:47
  • I have checked it, it works for few cases but not for others, like if I write any country in Africa, lets say 'Congo', it doesnt print anything. I guess, I need a custom script to do this. – Maverick Jul 31 '13 at 10:55
  • Sure, take a look at online services for getting currencies, e.g. http://www.webservicex.net/country.asmx. – alecxe Jul 31 '13 at 11:00
  • Ok, is there any package which gives me the continent name back when given the country name or country code? – Maverick Jul 31 '13 at 11:02
  • Thanks so much..I think I could use that to write a conditional code snippet and assign the currency :) – Maverick Jul 31 '13 at 11:18
  • @mad_programmer great! Consider posting your solution as an answer. – alecxe Jul 31 '13 at 15:38
3

django-countries just hands you a field to couple to your model (and a static bundle with flag icons). The field can hold a 2 character ISO from the list in countries.py which is convenient if this list is up-to-date (haven't checked) because it saves a lot of typing.

If you wish to create a model with verbose data that's easily achieved, e.g.

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 of preloading data, which could later be exported to create a fixture which is a nicer way of managing sample data.

from django_countries.countries import COUNTRIES

for key in dict(COUNTRIES).keys():
    Country.objects.create(iso=key)
Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100
3

I have just released country-currencies, a module that gives you a mapping of country codes to currencies.

>>> from country_currencies import get_by_country
>>> get_by_country('US')
('USD',)
>>> get_by_country('ZW')
('USD', 'ZAR', 'BWP', 'GBP', 'EUR')
Wil Tan
  • 701
  • 3
  • 4