pycountryパッケージからadminの国を設定するカスタムコマンドを作成しようとしています。しかし、私はそれを正しく参照していないと言っているAttributeErrorを混乱させています。誰かが私のミスを特定するのを助けることができますか?pycountry属性の国を設定するカスタムdjango管理コマンドAttributeError
管理コマンドのためのプロジェクトの設定は次のとおりです。
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
これは無関係ですが、以下の私がカスタマイズされてきた方法です。混乱を避けるために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'と' countries'とは何ですか? – Sayse