django import export app error: django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet

django import export app error: django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet

我正在将一个 Django 站点升级到 3.1.2,并注意到一个后端应用程序在询问 community help in fixing this I found out the app wasn't a custom made code by a single coder for the site but a community project called django-import-export 的过程中无法运行。因为它是最新的并且支持 django 3.1,所以我删除了手动将其设置为文件夹的方式并 pip 安装它。然后我尝试做一个 makemigrations 抛出了一个错误,在阅读了设置文档后假设你可能需要先做 collectstatic,它也抛出了同样的错误

(kpsga) sammy@kpsga:~/webapps/kpsga$ python manage.py collectstatic
Traceback (most recent call last):
File "manage.py", line 22, in <module>
    main()
File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/core/management/__init__.py", line 377, in execute
    django.setup()
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/apps/config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/sammy/webapps/kpsga/guides/models.py", line 77, in <module>
    class GuideResource(resources.ModelResource):
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/import_export/resources.py", line 930, in __new__
    field = new_class.field_from_django_field(f.name, f,
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/import_export/resources.py", line 1079, in field_from_django_field
    FieldWidget = cls.widget_from_django_field(django_field)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/import_export/resources.py", line 1045, in widget_from_django_field
    result = getattr(cls, result)(f)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/import_export/resources.py", line 1025, in get_fk_widget
    model=get_related_model(field))
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/import_export/resources.py", line 49, in get_related_model
    if hasattr(field, 'related_model'):
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/db/models/fields/related.py", line 95, in related_model
    apps.check_models_ready()
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/apps/registry.py", line 141, in check_models_ready
    raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.

这在某种程度上与 installed_apps 有关,所以我在下面包含了整个列表

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    #site apps
    'guides',
    'pages',

    #imported old dependencies
    'blog',
    'import_export',
    'generic',

    #imports
    'comments_moderation',
    'django_comments',
    'sorl.thumbnail',

    #additional django
    'django.contrib.sites',
    'django.contrib.humanize',
]

回溯显示在您的 models.py 中加载 GuideResource 时发生错误。

django-import-export 的 getting started docs 建议将资源放在 admin.py 中,所以我会尝试将 GuideResource 移到 models.py 之外。