Django-oscar: python manage.py oscar_populate_countries throws error -

Created on 5 Dec 2016  路  5Comments  路  Source: django-oscar/django-oscar

Hello,

while running the following command I get this error -

./manage.py oscar_populate_countries

or

python manage.py oscar_populate_countries -

This' the log.

./manage.py oscar_populate_countries
/Users/akos/oscar/lib/python2.7/site-packages/django/utils/six.py:808: RemovedInDjango110Warning: SubfieldBase has been deprecated. Use Field.from_db_value instead.
  return meta(name, bases, d)

/Users/akos/oscar/lib/python2.7/site-packages/django/core/management/__init__.py:345: RemovedInDjango110Warning: OptionParser usage for Django management commands is deprecated, use ArgumentParser instead
  self.fetch_command(subcommand).run_from_argv(self.argv)

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/akos/oscar/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/Users/akos/oscar/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/akos/oscar/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/akos/oscar/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/Users/akos/oscar/lib/python2.7/site-packages/oscar/management/commands/oscar_populate_countries.py", line 58, in handle
    for country in pycountry.countries]
  File "/Users/akos/oscar/lib/python2.7/site-packages/pycountry/db.py", line 22, in __getattr__
    raise AttributeError
AttributeError


I have been google to find the answer. And evening ended up creating a whole new project again. What Im I doing wrong?

Regards

Most helpful comment

As pycountry made new changes, the command oscar_populate_countries won't work.So for an alternate way for populating countries, please follow steps below.

-create a new file say populate_countries.py with the following code:-

# -*- coding: utf-8 -*-
import sys
from optparse import make_option

from django.core.management.base import BaseCommand, CommandError

from oscar.core.loading import get_model

Country = get_model('address', 'Country')


class Command(BaseCommand):
    help = "Populates the list of countries with data from pycountry."


    option_list = BaseCommand.option_list + (
        make_option(
            '--no-shipping',
            action='store_false',
            dest='is_shipping',
            default=True,
            help="Don't mark countries for shipping"),
        make_option(
            '--initial-only',
            action='store_true',
            dest='is_initial_only',
            default=False,
            help="Exit quietly without doing anything if countries were already populated."),
    )

    def handle(self, *args, **options):
        try:
            import pycountry
        except ImportError:
            raise CommandError(
                "You are missing the pycountry library. Install it with "
                "'pip install pycountry'")

        if Country.objects.exists():
            if options.get('is_initial_only', False):
                # exit quietly, as the initial load already seems to have happened.
                self.stdout.write("Countries already populated; nothing to be done.")
                sys.exit(0)
            else:
                raise CommandError(
                    "You already have countries in your database. This command "
                    "currently does not support updating existing countries.")

        countries = [
            Country(
                iso_3166_1_a2=country.alpha_2,
                iso_3166_1_a3=country.alpha_3,
                iso_3166_1_numeric=country.numeric,
                printable_name=country.name,
                name=getattr(country, 'official_name', ''),
                is_shipping_country=options['is_shipping'])
            for country in pycountry.countries]

        Country.objects.bulk_create(countries)
        self.stdout.write("Successfully added %s countries." % len(countries))


-move this file to your_apps_folder/management/commands. Make sure that you add __init__.py to management and commands folder.Refer this for help.

-after completing all the above steps, run command as python manage.py populate_country.This command will populate db with countries

All 5 comments

Same for me on a 100% fresh installation.

These are my installed packages in the venv:

$ pip freeze
Babel==2.3.4
Django==1.9
django-extra-views==0.6.4
django-haystack==2.5.1
django-oscar==1.3
django-tables2==1.0.7
django-treebeard==4.1.0
django-widget-tweaks==1.4.1
factory-boy==2.8.1
Faker==0.7.5
mock==2.0.0
pbr==1.10.0
phonenumbers==7.7.5
Pillow==3.4.2
purl==1.3
pycountry==16.11.27.1
python-dateutil==2.6.0
pytz==2016.10
six==1.10.0
sorl-thumbnail==12.4a1
Unidecode==0.4.19

UPDATE:

I reinstalled pycountry with the version specified on the requirements.txt (on the github repos), which is 1.8, and then everything ran without any errors. So maybe the preferred installation method is to checkout the github repos first, and then pip install from requirements.txt?

pycountry changed its API and its versioning scheme.
You can also make a fix for 1.3.

As pycountry made new changes, the command oscar_populate_countries won't work.So for an alternate way for populating countries, please follow steps below.

-create a new file say populate_countries.py with the following code:-

# -*- coding: utf-8 -*-
import sys
from optparse import make_option

from django.core.management.base import BaseCommand, CommandError

from oscar.core.loading import get_model

Country = get_model('address', 'Country')


class Command(BaseCommand):
    help = "Populates the list of countries with data from pycountry."


    option_list = BaseCommand.option_list + (
        make_option(
            '--no-shipping',
            action='store_false',
            dest='is_shipping',
            default=True,
            help="Don't mark countries for shipping"),
        make_option(
            '--initial-only',
            action='store_true',
            dest='is_initial_only',
            default=False,
            help="Exit quietly without doing anything if countries were already populated."),
    )

    def handle(self, *args, **options):
        try:
            import pycountry
        except ImportError:
            raise CommandError(
                "You are missing the pycountry library. Install it with "
                "'pip install pycountry'")

        if Country.objects.exists():
            if options.get('is_initial_only', False):
                # exit quietly, as the initial load already seems to have happened.
                self.stdout.write("Countries already populated; nothing to be done.")
                sys.exit(0)
            else:
                raise CommandError(
                    "You already have countries in your database. This command "
                    "currently does not support updating existing countries.")

        countries = [
            Country(
                iso_3166_1_a2=country.alpha_2,
                iso_3166_1_a3=country.alpha_3,
                iso_3166_1_numeric=country.numeric,
                printable_name=country.name,
                name=getattr(country, 'official_name', ''),
                is_shipping_country=options['is_shipping'])
            for country in pycountry.countries]

        Country.objects.bulk_create(countries)
        self.stdout.write("Successfully added %s countries." % len(countries))


-move this file to your_apps_folder/management/commands. Make sure that you add __init__.py to management and commands folder.Refer this for help.

-after completing all the above steps, run command as python manage.py populate_country.This command will populate db with countries

I had to hack the file, made changes to make to works some weeks back.

check this thread

https://groups.google.com/forum/#!topic/django-oscar/_Tfwlnvr43s

regards

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maerteijn picture maerteijn  路  5Comments

tfeldmann picture tfeldmann  路  5Comments

elioscordo picture elioscordo  路  8Comments

ad-65 picture ad-65  路  5Comments

lirlocker picture lirlocker  路  3Comments