导入视图时 django 抛出 "AppRegistryNotReady: Apps aren't loaded yet."

django throws "AppRegistryNotReady: Apps aren't loaded yet." when a view is imported

当我尝试将视图导入 Django 应用程序的 apps.py 模块时出现此错误。我正在使用 django 2.2.9、python 3.7.6、django-oscar 2.0.4。我已经覆盖了结帐应用程序,但此错误阻止我为所有 django oscar 应用程序添加视图或更改 url。是什么导致了这个错误?

(vsosci) c:\Users\anon\Documents\sosci.git>python manage.py check
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\anon\Envs\vsosci\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\anon\Envs\vsosci\lib\site-packages\django\core\management\__init__.py", line 357, in execute
    django.setup()
  File "C:\Users\anon\Envs\vsosci\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\anon\Envs\vsosci\lib\site-packages\django\apps\registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "C:\Users\anon\Envs\vsosci\lib\site-packages\django\apps\config.py", line 90, in create
    module = import_module(entry)
  File "C:\Users\anon\Envs\vsosci\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "c:\Users\anon\Documents\sosci.git\checkout\apps.py", line 3, in <module>
    from checkout.views import PaymentDetailsView
  File "c:\Users\anon\Documents\sosci.git\checkout\views.py", line 1, in <module>
    from oscar.apps.checkout.views import PaymentDetailsView
  File "C:\Users\anon\Envs\vsosci\lib\site-packages\oscar\apps\checkout\views.py", line 17, in <module>
    = get_classes('checkout.forms', ['ShippingAddressForm', 'ShippingMethodForm', 'GatewayForm'])
  File "C:\Users\anon\Envs\vsosci\lib\site-packages\oscar\core\loading.py", line 41, in get_classes
    return class_loader(module_label, classnames, module_prefix)
  File "C:\Users\anon\Envs\vsosci\lib\site-packages\oscar\core\loading.py", line 99, in default_class_loader
    oscar_module = _import_module(oscar_module_label, classnames)
  File "C:\Users\anon\Envs\vsosci\lib\site-packages\oscar\core\loading.py", line 133, in _import_module
    return __import__(module_label, fromlist=classnames)
  File "C:\Users\anon\Envs\vsosci\lib\site-packages\oscar\apps\checkout\forms.py", line 2, in <module>
    from django.contrib.auth.forms import AuthenticationForm
  File "C:\Users\anon\Envs\vsosci\lib\site-packages\django\contrib\auth\forms.py", line 10, in <module>
    from django.contrib.auth.models import User
  File "C:\Users\anon\Envs\vsosci\lib\site-packages\django\contrib\auth\models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "C:\Users\anon\Envs\vsosci\lib\site-packages\django\contrib\auth\base_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "C:\Users\anon\Envs\vsosci\lib\site-packages\django\db\models\base.py", line 103, in __new__
    app_config = apps.get_containing_app_config(module)
  File "C:\Users\anon\Envs\vsosci\lib\site-packages\django\apps\registry.py", line 252, in get_containing_app_config
    self.check_apps_ready()
  File "C:\Users\anon\Envs\vsosci\lib\site-packages\django\apps\registry.py", line 135, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

下面代码块中的第三行将导致抛出错误

import oscar.apps.checkout.apps as apps
from django.urls import re_path
from checkout.views import PaymentDetailsView

class CheckoutConfig(apps.CheckoutConfig):
    name = 'checkout'

    def ready(self):
        super().ready()
        self.paymentdetailsview = PaymentDetailsView

    def get_urls(self):
        urls = super().get_urls()

        urls += [
            re_path(r'payment-details/(?P<course_id>\d+)/$',
                self.paymentdetailsview.as_view(), name='payment-details'),
            re_path(r'payment-details/$',
                self.paymentdetailsview.as_view(), name='oscar-payment-details'),
            re_path(r'thank-you/$', self.thankyou_view.as_view(),
                name='thank-you'),            
        ]

        return self.post_process_urls(urls)

修改解决了我的问题

import oscar.apps.checkout.apps as apps
from django.urls import re_path


class CheckoutConfig(apps.CheckoutConfig):
    name = 'checkout'

    def ready(self):
        super().ready()
        from checkout import views as checkout_views
        self.checkout_views = checkout_views

    def get_urls(self):
        urls = super().get_urls()

        urls += [
            re_path(r'payment-details/(?P<course_id>\d+)/$',
                self.checkout_views.PaymentDetailsView.as_view(), name='payment-details'),
            re_path(r'payment-details/$',
                self.checkout_views.PaymentDetailsView.as_view(), name='oscar-payment-details'),
            re_path(r'thank-you/$', self.thankyou_view.as_view(),
                name='thank-you'),            
        ]

        return self.post_process_urls(urls)

来自docs

Although you can’t import models at the module-level where AppConfig classes are defined, you can import them in ready(), using either an import statement or get_model().

通过导入 PaymentDetailsView 您正在导入您的模型,因为 checkout.views 大概是在导入一些