importError:无法导入模型名称

时间:2018-04-10 12:54:10

标签: python-2.7 django-1.5 django-1.11

我正在将我的django应用程序从django1.5升级到django1.11。我知道这是一个巨大的跳跃。所以我收到了很多错误并尝试修复它。这是我的项目结构。我想我在appconfig中犯了错误。我无法弄明白。 enter image description here

现在我遇到了这个错误。ImportError: cannot import name TrainingProfile

settings.py

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

应用/管理/ apps.py

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

应用/帐户/ apps.py

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

应用/管理/模型/ 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

应用/管理/模型/ 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)

2 个答案:

答案 0 :(得分:2)

追溯中的相关部分是:

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',{ {1}},'account.TrainingProfile''admin.Company') 如果你无法替换所有这些,请至少更换一些可以解决问题的方法。

Django文档https://docs.djangoproject.com/en/1.11/ref/models/fields/#foreignkey

的更多信息

答案 1 :(得分:1)

您的模型位于名为training.py的文件中,而文件名根本未显示在导入中。它不在上面的文件结构中,所以只需仔细查看您的文件并修复您的import语句。