python - Django + Testypie 问题 : AppRegistryNotReadyException

标签 python django python-3.6 tastypie

我一直在尝试使用 Django 来测试 Testipie,但遇到了 AppRegistryNotReady 异常。我找到了一些方法来解决这个问题,但没有任何帮助。

  • Python:3.6
  • Django:2.1.2
  • django-tastypie:0.14.2

已安装的应用程序



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

services/__init__.py

`default_app_config = 'services.apps.ServicesConfig'`

services/apps.py



    from django.apps import AppConfig
    from django.contrib.auth import get_user_model
    from django.db.models import signals
    from tastypie.models import create_api_key
    User = get_user_model()
    class ServicesConfig(AppConfig):
        name = 'services'
        def ready(self):
            signals.post_save.connect(create_api_key, sender=User)

我得到了一个回溯:



    Traceback (most recent call last):
      File "/home/chumachenko/.local/share/virtualenvs/tastypie_tutorial-aq48eQFJ/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
        fn(*args, **kwargs)
      File "/home/chumachenko/.local/share/virtualenvs/tastypie_tutorial-aq48eQFJ/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
        autoreload.raise_last_exception()
      File "/home/chumachenko/.local/share/virtualenvs/tastypie_tutorial-aq48eQFJ/lib/python3.6/site-packages/django/utils/autoreload.py", line 248, in raise_last_exception
        raise _exception[1]
      File "/home/chumachenko/.local/share/virtualenvs/tastypie_tutorial-aq48eQFJ/lib/python3.6/site-packages/django/core/management/__init__.py", line 337, in execute
        autoreload.check_errors(django.setup)()
      File "/home/chumachenko/.local/share/virtualenvs/tastypie_tutorial-aq48eQFJ/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
        fn(*args, **kwargs)
      File "/home/chumachenko/.local/share/virtualenvs/tastypie_tutorial-aq48eQFJ/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
        apps.populate(settings.INSTALLED_APPS)
      File "/home/chumachenko/.local/share/virtualenvs/tastypie_tutorial-aq48eQFJ/lib/python3.6/site-packages/django/apps/registry.py", line 89, in populate
        app_config = AppConfig.create(entry)
      File "/home/chumachenko/.local/share/virtualenvs/tastypie_tutorial-aq48eQFJ/lib/python3.6/site-packages/django/apps/config.py", line 116, in create
        mod = import_module(mod_path)
      File "/home/chumachenko/.local/share/virtualenvs/tastypie_tutorial-aq48eQFJ/lib/python3.6/importlib/__init__.py", line 126, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
      File "", line 994, in _gcd_import
      File "", line 971, in _find_and_load
      File "", line 955, in _find_and_load_unlocked
      File "", line 665, in _load_unlocked
      File "", line 678, in exec_module
      File "", line 219, in _call_with_frames_removed
      File "/home/chumachenko/Documents/Projects/tastypie_tutorial/services/apps.py", line 4, in 
        from tastypie.models import create_api_key
      File "/home/chumachenko/.local/share/virtualenvs/tastypie_tutorial-aq48eQFJ/lib/python3.6/site-packages/tastypie/models.py", line 15, in 
        class ApiAccess(models.Model):
      File "/home/chumachenko/.local/share/virtualenvs/tastypie_tutorial-aq48eQFJ/lib/python3.6/site-packages/django/db/models/base.py", line 87, in __new__
        app_config = apps.get_containing_app_config(module)
      File "/home/chumachenko/.local/share/virtualenvs/tastypie_tutorial-aq48eQFJ/lib/python3.6/site-packages/django/apps/registry.py", line 249, in get_containing_app_config
        self.check_apps_ready()
      File "/home/chumachenko/.local/share/virtualenvs/tastypie_tutorial-aq48eQFJ/lib/python3.6/site-packages/django/apps/registry.py", line 132, in check_apps_ready
        raise AppRegistryNotReady("Apps aren't loaded yet.")
    django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

也许有人已经遇到过这个问题?

最佳答案

我已经找到了该问题的解决方案。您应该对 services/apps.py

进行一些更改


    from django.apps import AppConfig
    from django.contrib.auth import get_user_model
    from django.db.models import signals


    class ServicesConfig(AppConfig):
        name = 'services'

        # This line dispatches signal to Tastypie to create APIKey
        def ready(self):
            from tastypie.models import create_api_key
            User = get_user_model()
            signals.post_save.connect(create_api_key, sender=User)

关于python - Django + Testypie 问题 : AppRegistryNotReadyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53088153/

相关文章:

javascript - 如何根据文本变量选择 MultipleChoice 选项

python - urllib.error.HTTPError : HTTP Error 502: Bad Gateway PYTHON

python - 将 Python 枚举编码为 JSON

python - 使用openpyxl Python读取具有unicode名称的excel文件

python - 使用 api 在 slack 中将文件从一个用户共享给另一用户

python - 基于正则表达式解析路径 - Python

python - 值错误: Filename must be a string while uploading file to s3 bucket

python - Django CreateView 给出错误 "needs to have a value for field ".. ."before this many-to-many relationship can be used."

python - 使用 onefile 选项在 Pyinstaller 中添加数据文件

python - Choropleth 不绘制任何内容