pythondjangodjango-countries

ImportError: cannot import name CountryField


I already installed Django Countries using pip install django_countries and still I'm getting an error of ImportError: cannot import name CountryField:

(simpli)Allens-MacBook-Air:~/Sites/augmify/simpli [master]$ python manage.py syncdb
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/allenchun/.virtualenvs/simpli/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/Users/allenchun/.virtualenvs/simpli/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/allenchun/.virtualenvs/simpli/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/allenchun/.virtualenvs/simpli/lib/python2.7/site-packages/django/core/management/base.py", line 284, in execute
    self.validate()
  File "/Users/allenchun/.virtualenvs/simpli/lib/python2.7/site-packages/django/core/management/base.py", line 310, in validate
    num_errors = get_validation_errors(s, app)
  File "/Users/allenchun/.virtualenvs/simpli/lib/python2.7/site-packages/django/core/management/validation.py", line 34, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "/Users/allenchun/.virtualenvs/simpli/lib/python2.7/site-packages/django/db/models/loading.py", line 196, in get_app_errors
    self._populate()
  File "/Users/allenchun/.virtualenvs/simpli/lib/python2.7/site-packages/django/db/models/loading.py", line 78, in _populate
    self.load_app(app_name)
  File "/Users/allenchun/.virtualenvs/simpli/lib/python2.7/site-packages/django/db/models/loading.py", line 99, in load_app
    models = import_module('%s.models' % app_name)
  File "/Users/allenchun/.virtualenvs/simpli/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
    __import__(name)
  File "/Users/allenchun/Sites/augmify/simpli/members/models/__init__.py", line 2, in <module>
    from members.models.checkin import CheckIn
  File "/Users/allenchun/Sites/augmify/simpli/members/models/checkin.py", line 6, in <module>
    from merchants.models import MerchantLocation
  File "/Users/allenchun/Sites/augmify/simpli/merchants/models/__init__.py", line 3, in <module>
    from merchants.models.beacon import Beacon
  File "/Users/allenchun/Sites/augmify/simpli/merchants/models/beacon.py", line 5, in <module>
    from merchants.models.merchant_location import MerchantLocation
  File "/Users/allenchun/Sites/augmify/simpli/merchants/models/merchant_location.py", line 4, in <module>
    from django_countries import CountryField
ImportError: cannot import name CountryField

Solution

  • from django_countries.fields import CountryField
    

    Example usage the the pypi documentation.

    from django.db import models
    from django_countries.fields import CountryField
    
    class Person(models.Model):
        name = models.CharField(max_length=100)
        country = CountryField()
    
    >>> person = Person(name='Chris', country='NZ')
    >>> person.country
    Country(code='NZ')
    >>> person.country.name
    'New Zealand'
    >>> person.country.flag
    '/static/flags/nz.gif'