django countries currency code
Asked Answered
R

3

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!

Revolution answered 31/7, 2013 at 9:28 Comment(0)
T
16

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.

Toponymy answered 31/7, 2013 at 9:56 Comment(11)
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?Revolution
You are welcome. Mm, I don't think there is one particular module you need. pycountry operates on country (and country subdivisions too) level.Toponymy
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.Revolution
Yeah, this is because France has no country-specific currency. French Franc was withdrawn in 2002. FYI, pycountry uses ISO 4217 standard for currencies..Toponymy
How to define that the currency for France is EUR - this is a good and open question.Toponymy
@mad_programmer I've added 2 more options, please check.Toponymy
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.Revolution
Sure, take a look at online services for getting currencies, e.g. webservicex.net/country.asmx.Toponymy
Ok, is there any package which gives me the continent name back when given the country name or country code?Revolution
Thanks so much..I think I could use that to write a conditional code snippet and assign the currency :)Revolution
@mad_programmer great! Consider posting your solution as an answer.Toponymy
C
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)
Cauterize answered 31/7, 2013 at 9:44 Comment(3)
I dont want to create it. If that is the case then I have to create a long list.Revolution
You could easily write a script that creates the fixtures for you, it wouldn't take more then 2 minutes.Cauterize
And that is precisely what I am not able to understand how? :)Revolution
M
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')
Mikelmikell answered 28/12, 2014 at 14:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.