Django循环导入AppRegistryNotReady

2021-01-28 07:47:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我在虚拟环境中使用了django1.9.2和python3.4.2。在

我有很多应用程序,2个相关的是常见的和商店。在

普通/模型.py包含:

from django.apps import apps

class Document(CLDate):
    user = models.ForeignKey(User)
    assessmentorder = models.ForeignKey(apps.get_model('shop', 'AssessmentOrder'), blank=True, null=True)

商店/模型.py包含:

^{pr2}$

一。在

这是一个循环导入,我读了很多策略来解决它(包括apps.get_模型)但他们似乎都不适合我。我也试过了

apps.get_model('shop.AssessmentOrder')

,但还是一样。完整的错误消息如下:

File "/home/hidden/.virtualenvs/sccdb34/lib/python3.4/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/hidden/.virtualenvs/sccdb34/lib/python3.4/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/home/hidden/.virtualenvs/sccdb34/lib/python3.4/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/home/hidden/.virtualenvs/sccdb34/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/hidden/.virtualenvs/sccdb34/lib/python3.4/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/hidden/.virtualenvs/sccdb34/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/hidden/.virtualenvs/sccdb34/lib/python3.4/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/home/hidden/.virtualenvs/sccdb34/lib/python3.4/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/home/hidden/.virtualenvs/sccdb34/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/home/hidden/git/sccdb/sccdb/common/models.py", line 25, in <module>
class Document(CLDate):
File "/home/hidden/git/sccdb/sccdb/common/models.py", line 28, in Document
assessmentorder = models.ForeignKey(apps.get_model('shop.AssessmentOrder'), blank=True, null=True)
File "/home/hidden/.virtualenvs/sccdb34/lib/python3.4/site-packages/django/apps/registry.py", line 194, in get_model
self.check_models_ready()
File "/home/hidden/.virtualenvs/sccdb34/lib/python3.4/site-packages/django/apps/registry.py", line 131, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.

它是否与我的django版本或python3有关,或者我做错了什么?在

2条回答
网友
1楼 ·

不用在外键声明中使用get_model函数,您只需将模型名称作为字符串,它仍然可以工作:

assessmentorder = models.ForeignKey('shop.AssessmentOrder', blank=True, null=True)

这应该可以解决这个问题

网友
2楼 ·

不要包括模型,只需给它们设置一条路径。如docs中所述

from django.conf import settings

class Document(CLDate):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    assessmentorder = models.ForeignKey('shop.AssessmentOrder', blank=True, null=True)

以及

^{pr2}$

相关问题