繁体   English   中英

尝试将模型从一个应用程序迁移到另一个应用程序时,Django导入错误

[英]Django import error when trying to migrate models from app to app

我在导入时遇到了奇怪的问题

自从我的一些应用变得非常庞大以来,我已经创建了新的应用并尝试将一些模块移到那里。

我创建了新的应用程序产品,并将其添加到我的设置文件中,并写在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

可能是什么问题呢 ?

这是我在item中的ItemGroup模型

@with_author  
class ItemGroup(models.Model):
    name = models.CharField(max_length=30)
    description = models.TextField(null=True, blank=True)
    creation_time = models.DateTimeField(auto_now_add=True)
    subcategory = models.ForeignKey(SubCategory, on_delete=models.PROTECT)
    keywords = models.CharField(max_length=50,null=True, blank=True) 
    #hierarchy = TreeForeignKey(Hierarchy,  blank=True, null=True, related_name='cat') 
    def __unicode__(self):
        return u'%s %s' % ( self.id, self.name)

您尝试进行交叉导入。 那是不允许的。 如果不需要,请"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某些item.models正在尝试从product.models导入某些内容,而product.models某些内容正在尝试导入item.models某些item.models ...

解决方案是完全避免它,方法是允许Django处理循环关系而不是Python, 就像在此问题的答案中一样

因此,在您的情况下,请在此处删除导入,而应像字符串一样引用它:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM