Python 尝试将模型从一个应用迁移到另一个应用时出现Django导入错误

Python 尝试将模型从一个应用迁移到另一个应用时出现Django导入错误,python,django,Python,Django,我在进口方面遇到了奇怪的问题 我创建了一个新的应用程序,并尝试将一些模块移动到那里,因为我的一些应用程序变得非常大 我创建了新的应用程序产品,并将其添加到我的设置文件中,并在product/model.py中写入 from django.db import models from author.decorators import with_author from item.models import ItemGroup from item.models import Material # Cr

我在进口方面遇到了奇怪的问题

我创建了一个新的应用程序,并尝试将一些模块移动到那里,因为我的一些应用程序变得非常大

我创建了新的应用程序产品,并将其添加到我的设置文件中,并在product/model.py中写入

from django.db import models
from author.decorators import with_author
from item.models import ItemGroup
from item.models import Material

# Create your models here.
@with_author  
class Product(models.Model):
    code = models.CharField(max_length=30, unique=True)
    name = models.CharField(max_length=30)
    description = models.TextField(null=True, blank=True)
    creation_time = models.DateTimeField(auto_now_add=True, blank=True)
    itemgroup = models.ForeignKey(ItemGroup, on_delete=models.PROTECT)
    material = models.ForeignKey(Material, on_delete=models.PROTECT)
    keywords = models.CharField(max_length=50,null=True, blank=True)
    valid_from = models.DateTimeField(null=True, blank=True)
    valid_to = models.DateTimeField(null=True, blank=True)
    style1 = models.CharField(max_length=30,null=True, blank=True)
    style2 = models.CharField(max_length=30,null=True, blank=True)
    style3 = models.CharField(max_length=30,null=True, blank=True)
    size = models.CharField(max_length=30,null=True, blank=True)
    dimension = models.CharField(max_length=30,null=True, blank=True)
    color = models.CharField(max_length=30,null=True, blank=True)

    def __unicode__(self):
        return u'%s %s %s' % ( self.id, self.code, self.name)
当我执行

>python manage.py makemigrations item

>python manage.py makemigrations product
我犯了一个错误

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\I812624\dev\mrp\lib\site-packages\django\core\management\__ini
__.py", line 338, in execute_from_command_line
    utility.execute()
  File "C:\Users\I812624\dev\mrp\lib\site-packages\django\core\management\__ini
__.py", line 312, in execute
    django.setup()
  File "C:\Users\I812624\dev\mrp\lib\site-packages\django\__init__.py", line 18
 in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\I812624\dev\mrp\lib\site-packages\django\apps\registry.py", li
e 108, in populate
    app_config.import_models(all_models)
  File "C:\Users\I812624\dev\mrp\lib\site-packages\django\apps\config.py", line
198, in import_models
    self.models_module = import_module(models_module_name)
  File "c:\python27\Lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "C:\Users\I812624\dev\mrp\src\item\models.py", line 13, in <module>
    from product.models import Product
  File "C:\Users\I812624\dev\mrp\src\product\models.py", line 3, in <module>
    from item.models import ItemGroup
ImportError: cannot import name ItemGroup

您尝试进行交叉导入。这是不允许的。如果不需要,请注释掉
“C:\Users\I812624\dev\mrp\src\item\models.py”
中的第13行

或者注释掉以下内容中的第3行和第4行:

"C:\Users\I812624\dev\mrp\src\product\models.py"
并且做:

itemgroup = models.ForeignKey("item.ItemGroup", on_delete=models.PROTECT)
material = models.ForeignKey("item.Material", on_delete=models.PROTECT)

我猜你们有循环进口。
item.models
中的某些内容正试图从
product.models
中导入某些内容,后者正试图导入
item.models中的某些内容

解决方案是通过允许Django处理循环关系而不是Python来完全避免这种情况

因此,在您的情况下,请删除此处的导入,并将其作为字符串引用:

@with_author  
class ItemGroup(models.Model):
    # ...
    subcategory = models.ForeignKey('product.SubCategory', on_delete=models.PROTECT)
    # ...

看起来问题是因为循环进口。你能用item/models.py更新帖子吗?想知道为什么在it中导入产品模型。
@with_author  
class ItemGroup(models.Model):
    # ...
    subcategory = models.ForeignKey('product.SubCategory', on_delete=models.PROTECT)
    # ...