Python 使用pycountry AttributeError填充国家/地区的自定义django管理命令
我正在尝试创建一个自定义命令,以便从pycountry包在admin中填充国家。但是,我越来越困惑的属性错误,说我没有正确地引用它。有人能帮我找出我的错误吗 管理命令的项目设置为:Python 使用pycountry AttributeError填充国家/地区的自定义django管理命令,python,django,python-2.7,Python,Django,Python 2.7,我正在尝试创建一个自定义命令,以便从pycountry包在admin中填充国家。但是,我越来越困惑的属性错误,说我没有正确地引用它。有人能帮我找出我的错误吗 管理命令的项目设置为: market/ manage.py address/ __init__.py models.py management/ __init__.py commands/ __i
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:\Users\Alikhan\amazonclone\market>manage.py market_populate_countries
Traceback (most recent call last):
File "C:\Users\Alikhan\amazonclone\market\manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
354, in execute_from_command_line
utility.execute()
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
346, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 394,
in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 445,
in execute
output = self.handle(*args, **options)
File "C:\Users\Alikhan\amazonclone\market\address\management\commands\market_
populate_countries.py", line 56, in handle
for country in pycountry.countries]
File "C:\Python27\lib\site-packages\pycountry\db.py", line 22, in __getattr__
raise AttributeError
AttributeError
似乎将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]
这就是全部错误吗?它真的没有说是什么属性导致了错误吗?是的,这对我来说是令人惊讶的。我可以在shell中参考,并可以查看国家列表。不在代码中工作。什么是
pycountry
和countries
?它是一个包,并且是一个维护包。Country是“address”应用程序中的一个模型,用于从pycountry软件包中提取和保存国家。@Alikhan-您确定您同时更改了这两个国家吗?我收到了与您相同的错误,在更改之前和之后,它工作正常-我已仔细检查,是的,这是相同的错误。让我再试一次,重新编写整个代码,这样我就可以看到任何可能的错误。@Alikhan-我的测试代码和你的测试代码之间唯一可能的不同之处是你正在使用的pycountry
版本(我刚刚安装了pip-pycountry)和选项['is_-shipping']
,其中我刚刚替换为False,尝试在您自己的shell中使用我的输入,看看是否works@Alikhan-我刚刚做了pip安装pycountry
,然后pip安装pycountry==1.10
,我不知道还能帮多少忙,尝试使用我以前的pastebinI中的代码在python控制台中自己复制错误,我按照建议添加了完整的代码,并且成功了。在我刚刚添加一行之前。谢谢你的帮助,我真的很感激。
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]