Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 355
Maverick Avatar asked Jul 31 '13 09:07

Maverick


People also ask

How can I get country code in Django?

Get the countries from PythonUse the django_countries. countries object instance as an iterator of ISO 3166-1 country codes and names (sorted by name).


3 Answers

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.

like image 94
alecxe Avatar answered Oct 19 '22 14:10

alecxe


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)
like image 34
Hedde van der Heide Avatar answered Oct 19 '22 14:10

Hedde van der Heide


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')
like image 36
Wil Tan Avatar answered Oct 19 '22 13:10

Wil Tan