尝试将模型从app迁移到app时,Django导入错误

时间:2016-05-02 15:35:14

标签: 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

# 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

可能是什么问题?

这是我在项目

中的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)

2 个答案:

答案 0 :(得分:1)

您尝试进行交叉导入。这是不允许的。如果您不需要,请在"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)

答案 1 :(得分:1)

我在这里猜测你正在进行循环导入。 item.models中的某些内容正在尝试从product.models导入某些内容,该内容正尝试导入item.models中的内容...

解决方法是完全避免它,允许Django处理循环关系而不是Python,like in the answer to this question

所以在你的情况下,在这里删除导入,而不是像字符串一样引用它:

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