2015-02-28 4 views
1

Я импортирую модели из разных приложений, но я получаю эту ошибку, и я не уверен, почему это происходит.ImportError из разных приложений

Traceback (most recent call last): 
    File "manage.py", line 10, in <module> 
    execute_from_command_line(sys.argv) 
    File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line 
    utility.execute() 
    File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 354, in execute 
    django.setup() 
    File "/Library/Python/2.7/site-packages/django/__init__.py", line 21, in setup 
    apps.populate(settings.INSTALLED_APPS) 
    File "/Library/Python/2.7/site-packages/django/apps/registry.py", line 108, in populate 
    app_config.import_models(all_models) 
    File "/Library/Python/2.7/site-packages/django/apps/config.py", line 202, in import_models 
    self.models_module = import_module(models_module_name) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module 
    __import__(name) 
    File "/Users/bli1/Development/Django/Spark/users/models.py", line 5, in <module> 
    from notifications.models import Notification 
    File "/Users/bli1/Development/Django/Spark/notifications/models.py", line 2, in <module> 
    from users.models import UserProfile 
ImportError: cannot import name UserProfile 

мои уведомления/модели:

from django.db import models 
from users.models import UserProfile 
from restaurants.models import Restaurant 

class Notification(models.Model): 
    user = models.ForeignKey(UserProfile) 
    name = models.CharField(max_length=100) 
    about = models.TextField(max_length=1024, blank=True) 
    restaurant = models.ForeignKey(Restaurant) 

    def __str__(self): 
     return self.name 

мои пользователи/models.py:

from django.db import models 
from django.contrib.auth.models import User 
from notifications.models import Notification 

class UserProfile(models.Model): 
    user = models.OneToOneField(User) 
    bio = models.TextField(max_length=1024, null=True, blank=True) 
+0

@ozgur да это – Liondancer

+0

хорошо, увидеть мой ответ ниже. – ozgur

ответ

1

Вы стали жертвой круговой зависимости от импорта. Для того, чтобы решить эту проблему, вы должны сказать Django путь импорта модели в ForeignKey вместо фактически импортирования:

class Notification(models.Model): 
    user = models.ForeignKey('users.UserProfile') 

Также не забудьте удалить оператор импорта из from users.models import UserProfile из уведомлений/models.py.

1

У вас есть цикл циклических зависимостей.

User импорт Notification и Notification импорт User.

Просто удалите from notifications.models import Notification из вашего users/models.py

Смежные вопросы