【问题标题】:How to solve the circular import error in django?如何解决django中的循环导入错误?
【发布时间】:2017-11-19 22:58:33
【问题描述】:

我知道该错误可能是由于循环导入错误,但对此知之甚少,我无法纠正它。我曾尝试使用类似问题中给出的方法,但无法解决。该项目有两个应用程序咨询和主要应用程序,我需要将它们的模型相互导入,

咨询/models.py

from django.db import models
from django.contrib.auth.models import User
from main.models import Customer


class Question(models.Model):
    name = models.ForeignKey(Customer, on_delete=models.CASCADE)
    type = models.CharField(max_length=100, default="SkinCare")
    title = models.CharField(max_length=1000)
    body = models.CharField(max_length=1000000)
    image = models.FileField(blank=True, default=None)
    time = models.DateTimeField()
    deltatime = models.IntegerField(default=0)

    def __str__(self):
        return str(self.time)


class Reply(models.Model):
    name = models.ForeignKey(Question, on_delete=models.CASCADE)
    user = models.ForeignKey(Customer, on_delete=models.CASCADE)
    text = models.CharField(max_length=10000000000)
    like = models.IntegerField(default=0)
    dislike = models.IntegerField(default=0)
    time = models.DateTimeField()
    deltatime = models.IntegerField(default=0)

    def __str__(self):
        return str(self.time)

main/models.py

from django.contrib.auth.models import User
from django.db import models
from consult.models import Question, Reply


class Customer(models.Model):
    name = models.ForeignKey(User, null=True)
    gender = models.CharField(max_length=100)
    skin_type = models.CharField(max_length=1000)
    hair_type = models.CharField(max_length=1000)
    bookmarked = models.ManyToManyField(Question)

    def __str__(self):
        return str(self.name)

当我运行尝试迁移应用程序时,出现以下错误:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\Nikhil Khandelwal\AppData\Local\Programs\Python\Python35-
32\lib\site-packages\django-1.10.4-
py3.5.egg\django\core\management\__init__.py", line 367, in execute_from_
command_line
    utility.execute()
  File "C:\Users\Nikhil Khandelwal\AppData\Local\Programs\Python\Python35-
32\lib\site-packages\django-1.10.4-
py3.5.egg\django\core\management\__init__.py", line 341, in execute
django.setup()
  File "C:\Users\Nikhil Khandelwal\AppData\Local\Programs\Python\Python35-
32\lib\site-packages\django-1.10.4-py3.5.egg\django\__init__.py", line 27, 
in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\Nikhil Khandelwal\AppData\Local\Programs\Python\Python35-
32\lib\site-packages\django-1.10.4-py3.5.egg\django\apps\registry.py", line 
108, in populate
    app_config.import_models(all_models)
  File "C:\Users\Nikhil Khandelwal\AppData\Local\Programs\Python\Python35-
32\lib\site-packages\django-1.10.4-py3.5.egg\django\apps\config.py", line 
199, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Users\Nikhil Khandelwal\AppData\Local\Programs\Python\Python35-
32\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in 
_call_with_frames_removed
  File "C:\New folder\WebD\zerovey\consult\models.py", line 3, in <module>
    from main.models import Customer
  File "C:\New folder\WebD\zerovey\main\models.py", line 3, in <module>
    from consult.models import Question, Reply
ImportError: cannot import name 'Question'

考虑到我是Django的初学者,请回答,在此先感谢您:)

【问题讨论】:

  • 请添加错误的回溯。
  • 不要直接导入模型,只导入应用 exa - import main 然后访问模型 exa - main.models.Customer

标签: python django python-3.x django-models django-views


【解决方案1】:

在外键和多对多字段中使用to='&lt;app_lable&gt;.&lt;Model Name&gt;'

从文件中删除导入模型,在模型中添加外键和多字段,就像我在下面的代码中所做的那样。to='consult.Question' 当我们从 makemifration 命令创建迁移时,在迁移文件中使用硬编码模型名称,因此使用相同的方式编写 Foreignkey 和 Manytomany 字段

from django.contrib.auth.models import User 
from django.db import models
class Customer(models.Model): 
    name = models.ForeignKey(User, null=True) 
    gender = models.CharField(max_length=100)
    skin_type = models.CharField(max_length=1000)
    hair_type = models.CharField(max_length=1000)
    bookmarked = models.ManyToManyField(to='consult.Question')

    def __str__(self):
        return str(self.name)

【讨论】:

  • 4 年后,我从这个答案中受益。向你致敬@neeraj-kumar
【解决方案2】:

尝试将您的导入更改为: 在咨询/models.py

import main.models.Customer

在 main/models.py 中

import consult.models.Question
import consult.models.Reply

然后你使用main.models.Customer而不是Customer,而不是QuestionReply你使用import consult.models.Questionconsult.models.Reply

【讨论】:

  • 我尝试使用您的建议@omu_negru,但收到以下错误:-http://dpaste.com/1MZ9N6R
  • 我也尝试过“main import models as mm”,但它给出了错误“AttributeError: module 'consult.models' has no attribute 'Question'”
  • 尝试删除as mm,然后使用完全限定的导入路径并告诉我它是否有效
  • 删除 mm 会导致错误,因为现在已经导入了两个模型 - 从 django.db 导入模型和从主要导入模型
【解决方案3】:

解决循环导入的另一种方法是:

from django.apps import apps
MyModel = apps.get_model('myapp', 'MyModel')

【讨论】:

    猜你喜欢
    • 2020-11-01
    • 2012-04-19
    • 1970-01-01
    • 2014-12-10
    • 2023-03-05
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    相关资源
    最近更新 更多