我正在尝试创建一个自定义命令以从pycountry包填充admin中的国家/地区。但是我感到困惑 属性错误 说我没有正确引用它。有人可以帮助我识别我的错误吗?
管理命令的项目设置为:
market/
manage.py
address/
__init__.py
models.py
management/
__init__.py
commands/
__init__.py
market_populate_countries.py
views.py
管理命令代码:
from __future__ import absolute_import
import sys
from optparse import make_option
from default.core.loading import get_model
from django.core.management.base import BaseCommand, CommandError
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.alpha2,
iso_3166_1_a3=country.alpha3,
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))
追踪:
C:UsersAlikhanamazonclonemarket>manage.py market_populate_countries
Traceback (most recent call last):
File "C:UsersAlikhanamazonclonemarketmanage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:Python27libsite-packagesdjangocoremanagement__init__.py", line
354, in execute_from_command_line
utility.execute()
File "C:Python27libsite-packagesdjangocoremanagement__init__.py", line
346, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:Python27libsite-packagesdjangocoremanagementbase.py", line 394,
in run_from_argv
self.execute(*args, **cmd_options)
File "C:Python27libsite-packagesdjangocoremanagementbase.py", line 445,
in execute
output = self.handle(*args, **options)
File "C:UsersAlikhanamazonclonemarketaddressmanagementcommandsmarket_
populate_countries.py", line 56, in handle
for country in pycountry.countries]
File "C:Python27libsite-packagespycountrydb.py", line 22, in __getattr__
raise AttributeError
AttributeError
这无关紧要,但以下是我为get_model自定义源函数以避免任何混淆的方式。
from __future__ import absolute_import
import sys
import traceback
from importlib import import_module
from django.apps import apps
from django.apps.config import MODELS_MODULE_NAME
from django.conf import settings
from django.core.exceptions import AppRegistryNotReady
from .exceptions import (
AppNotFoundError, ClassNotFoundError, ModuleNotFoundError)
def get_model(app_label, model_name):
try:
return apps.get_model(app_label, model_name)
except AppRegistryNotReady:
if apps.apps_ready and not apps.models_ready:
app_config = apps.get_app_config(app_label)
import_module('%s.%s' % (app_config.name, MODELS_MODULE_NAME))
return apps.get_registered_model(app_label, model_name)
else:
raise
pycountry
似乎将数字分隔为 alpha_2 和 3
country.alpha2
和country.alpha3
应分别country.alpha_2
和country.alpha_3
更改上述内容后,您的代码应变为
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]