我知道我的问题可能涉及相互/循环导入,并且我在发布之前确实进行了搜索。我发现当前解决我的问题的唯一解决方案是将导入移至其中一个文件的末尾,就在实际使用导入的函数之前。但我也读到这是非常不推荐的。
推荐的解决方案,只需执行以下操作:
in A.py
-> import B
and in B.py
-> import A
然后再访问该功能,还是不行。
所以我的 Django 应用程序中有三个应用程序:“core”、“notifications”和“accounts”。以下是他们的片段:
核心模型:
from django.db import models
from django.contrib.auth.models import User
import notifications.models
from z_misc import general_scripts
# Create your models here.
class BaseModel(models.Model):
created_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
这是我的使用方法Notification
in core.models
:
# inspect mb for each artist
### Assume class is defined and @classmethod is intended, having some formatting pains here
@classmethod
def refresh_from_mb(cls):
artists = cls.get_artists_with_subs()
for artist in artists:
added_rgs = general_scripts.refresh_artist(artist.mbid)
for rg in added_rgs:
new_notification = Notification(artist_id=artist.id, release_group=rg)
new_notification.save()
通知.模型:
from django.db import models
import core.models
import accounts.models
class Notification(core.models.BaseModel):
artist_id = models.OneToOneField(core.models.Artist, related_name="artist_notifications", null=False)
release_group = models.OneToOneField(core.models.ReleaseGroup, related_name="rg_notifications", null=False)
def get_rg_type(self):
return self.release_group.type
账户.模型:
from django.db import models
from django.contrib.auth.models import User
import core.models
from django.db.models import Q
# Create your models here.
class UserProfile(core.models.BaseModel):
#TODO: add email_activated = models.BooleanField(default=False)
user = models.OneToOneField(User, related_name="user_profile")
正如你所看到的,我遵循建议而不是这样做from
进而import
,但更确切地说import
然后使用完整的符号。这不起作用。完整的错误回溯是:
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10fa9af28>
Traceback (most recent call last):
File "/Users/username/.virtualenvs/bap_dev/lib/python3.6/site-packages/django/utils/autoreload.py", line 228, in wrapper
fn(*args, **kwargs)
File "/Users/username/.virtualenvs/bap_dev/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
autoreload.raise_last_exception()
File "/Users/username/.virtualenvs/bap_dev/lib/python3.6/site-packages/django/utils/autoreload.py", line 251, in raise_last_exception
six.reraise(*_exception)
File "/Users/username/.virtualenvs/bap_dev/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/Users/username/.virtualenvs/bap_dev/lib/python3.6/site-packages/django/utils/autoreload.py", line 228, in wrapper
fn(*args, **kwargs)
File "/Users/username/.virtualenvs/bap_dev/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/username/.virtualenvs/bap_dev/lib/python3.6/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models()
File "/Users/username/.virtualenvs/bap_dev/lib/python3.6/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/Users/username/.pyenv/versions/3.6.1/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "/Users/username/PycharmProjects/artist_notify/core/models.py", line 3, in <module>
import notifications.models
File "/Users/username/PycharmProjects/artist_notify/notifications/models.py", line 3, in <module>
import accounts.models
File "/Users/username/PycharmProjects/artist_notify/accounts/models.py", line 8, in <module>
class UserProfile(core.models.BaseModel):
AttributeError: module 'core' has no attribute 'models'
我不确定此时我能做些什么来解决这个问题。除了开始将导入语句移动到文件的中间/末尾,这似乎是一个很大的禁忌。
请不要建议我使用from app.models import ModelName
,因为这就是我之前使用的,直到由于循环导入而开始出现错误。