1

I am upgrading my django application from django1.5 to django1.11. I know its a huge jump. So I am getting so many errors and try to get it fixed. This is my project structure. I think I have made mistakes in appconfig. I couldn't figure it out. enter image description here

Now I am stuck on this error.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

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)
Akhi
  • 221
  • 1
  • 3
  • 14

2 Answers2

5

The relevant part in your traceback is this:

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

From here I see that you are importing admin models from account models (Company and Track) and vice versa (IndustryProfile, TrainingProfile) which is making a circular import. It's very strange that this was working in Django 1.5 at all.

To fix your problem you can check how you are using these models and if the only thing is to put it as argument to models.ForeignKey you can remove the import and use string instead ('account.IndustryProfile', 'account.TrainingProfile', 'admin.Company' and 'admin.Track') If you can't replace all of them try to replace at least these that will fix your problem.

More info at Django documentation https://docs.djangoproject.com/en/1.11/ref/models/fields/#foreignkey

VStoykov
  • 1,705
  • 12
  • 15
  • I changed my `ForeignKey` field to `provider = models.ForeignKey('account.TrainingProfile', db_index=True)`. same like I changed the rest of the fields in admin app. But now I am getting `RuntimeError: Conflicting 'userprofile' models in application 'account': and .` I think I have made some mistake in appConfig. But i couldn't figure it out. – Akhi Apr 11 '18 at 06:05
  • You need to get rid of the `app_label` in the `Meta`. In 1.11 you don't need them. Probably there is the problem. – VStoykov Apr 13 '18 at 16:00
1

Your model is in a file named training.py while the file name is not presented in the import at all. It is not in the file structure above neither, so just take a good look at your files and fix your import statement.

Ilian Iliev
  • 3,217
  • 4
  • 26
  • 51
  • It was working fine in 1.5.5. It showing error only when I am upgrading django and `TrainingProfile` model is in account/models.py file. – Akhi Apr 10 '18 at 14:23
  • Could it be that another import in the `models.py` file is breaking, which makes the model not importable? Try `from account.models import *` – Ilian Iliev Apr 10 '18 at 14:28