importError: cannot import model name

importError: cannot import model name

我正在将我的 Django 应用程序从 django1.5 升级到 django1.11。我知道这是一个巨大的飞跃。所以我遇到了很多错误并尝试修复它。这是我的项目结构。我想我在 appconfig 中犯了错误。我想不通。

现在我卡在这个错误上了。ImportError: cannot import name TrainingProfile

settings.py

INSTALLED_APPS = (
'admin.apps.AdminConfig',
'account.apps.AccountConfig',
'.............'
)

apps/admin/apps.py

class AdminConfig(AppConfig):
    name = 'apps.admin'
    label = 'admin_app'

apps/account/apps.py

class AccountConfig(AppConfig):
    name = 'apps.account'
    label = 'account_app'

apps/admin/models/init.py

from apps.admin.models.sector import *
from apps.admin.models.track import *
from apps.admin.models.training import *
...............

回溯

Traceback (most recent call last):
  File "/home/sample-applications/upgrade/venv/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/home/sample-applications/upgrade/venv/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
    autoreload.raise_last_exception()
  File "/home/sample-applications/upgrade/venv/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 251, in raise_last_exception
    six.reraise(*_exception)
  File "/home/sample-applications/upgrade/venv/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/home/sample-applications/upgrade/venv/local/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/sample-applications/upgrade/venv/local/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models()
  File "/home/sample-applications/upgrade/venv/local/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/sample-applications/upgrade/pursuite/django-pursuite/apps/admin/models/__init__.py", line 17, in <module>
    from apps.admin.models.job import *
  File "/home/sample-applications/upgrade/pursuite/django-pursuite/apps/admin/models/job.py", line 13, in <module>
    from account.models import IndustryProfile
  File "./apps/account/models.py", line 13, in <module>
    from admin.models import Company, Track
  File "./apps/admin/models/__init__.py", line 18, in <module>
    from apps.admin.models.training import *
  File "/home/sample-applications/upgrade/pursuite/django-pursuite/apps/admin/models/training.py", line 12, in <module>
    from account.models import TrainingProfile
ImportError: cannot import name TrainingProfile

apps/admin/models/training.py

from django.db import models
from django.contrib import admin
from django.core.urlresolvers import reverse
from tinymce.models import HTMLField
from account.models import TrainingProfile
from analytics.models import State
from admin.common import html2text

__all__ = ['Training']


class Training(models.Model):
    '''
        Training
    '''
    class Meta:
        '''
            Meta properties for this model
        '''
        app_label = 'admin'

    TRAINING_CHOICES = {
        ('T', 'Trainers'),
        ('S', 'Students'),
    }
    training_title = models.CharField(max_length=100, db_index=True)
    provider = models.ForeignKey(TrainingProfile, db_index=True)
    training_for = models.CharField(max_length=1, choices=TRAINING_CHOICES)
    description = HTMLField()
    location = models.ForeignKey(State, db_index=True)
    create_date = models.DateTimeField(auto_now_add=True)
    write_date = models.DateTimeField(auto_now=True)

您的模型位于名为 training.py 的文件中,而文件名根本没有出现在导入中。它也不在上面的文件结构中,因此请仔细查看您的文件并修复您的导入语句。

你的回溯中的相关部分是这样的:

File "/home/sample-applications/upgrade/pursuite/django-pursuite/apps/admin/models/__init__.py", line 17, in <module>
    from apps.admin.models.job import *
  File "/home/sample-applications/upgrade/pursuite/django-pursuite/apps/admin/models/job.py", line 13, in <module>
    from account.models import IndustryProfile
  File "./apps/account/models.py", line 13, in <module>
    from admin.models import Company, Track
  File "./apps/admin/models/__init__.py", line 18, in <module>
    from apps.admin.models.training import *
  File "/home/sample-applications/upgrade/pursuite/django-pursuite/apps/admin/models/training.py", line 12, in <module>
    from account.models import TrainingProfile
ImportError: cannot import name TrainingProfile

从这里我看到您正在从帐户模型(CompanyTrack)导入管理模型,反之亦然(IndustryProfileTrainingProfile)循环导入。很奇怪,这在 Django 1.5 中完全有效。

要解决你的问题,你可以检查你是如何使用这些模型的,如果唯一的事情是把它作为参数 models.ForeignKey 你可以删除导入并使用字符串代替('account.IndustryProfile' , 'account.TrainingProfile', 'admin.Company''admin.Track') 如果您不能更换所有这些,请尝试至少更换这些可以解决您的问题。

有关 Django 文档的更多信息 https://docs.djangoproject.com/en/1.11/ref/models/fields/#foreignkey