pythondjangodjango-modelscountry

Country choice for users in Django


I'm not sure if this is the right place to ask this or Programmers SE so please point it out if I am wrong to post it on here.

I need to make a UserProfile model that contains extra information about users in Django.

But one of the information that it contains is user's home country.

As you can imagine, the list of all the countries in the world is.. quite a BIG list. (http://www.state.gov/misc/list/)

Is there any way (the lazy way) that I can have this function without listing all the countries by myself like this?

COUNTRY_CHOICE = (('Afghanistan', 'Afghanistan'), ('Albania','Albania') .....

Solution

  • There is a reusable app django-countries. Among other features it provides a CountryField:

    A country field for Django models that provides all ISO 3166-1 countries as choices.

    CountryField is based on Django's CharField, providing choices corresponding to the official ISO 3166-1 list of countries (with a default max_length of 2).

    from django.db import models
    from django_countries.fields import CountryField
    
    class Person(models.Model):
        name = models.CharField(max_length=100)
        country = CountryField()