【问题标题】:Django import error when trying to migrate models from app to app尝试将模型从应用迁移到应用时出现 Django 导入错误
【发布时间】:2016-08-27 10:36:36
【问题描述】:

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

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

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

【问题讨论】:

  • 看起来问题是由于循环导入。你能用 item/models.py 更新帖子吗?想知道为什么要在里面导入Product模型。

标签: python django


【解决方案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)

【讨论】:

    【解决方案2】:

    我在这里猜测您正在进行循环导入。 item.models 中的某些内容正在尝试从 product.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)
        # ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-24
      • 1970-01-01
      • 2010-10-27
      • 2023-03-06
      • 2021-12-29
      • 1970-01-01
      • 2021-04-09
      • 2015-10-10
      相关资源
      最近更新 更多