如何解决Django中的循环导入错误?

2024-09-27 07:24:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道这个错误可能是由于循环导入错误造成的,但是我对它没有太多的了解,所以我无法更正它。我试过使用类似问题中给出的方法,但无法解决它。那个项目有两个应用程序consult和main,我需要将它们的模型导入彼此

咨询/模型.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)

主/模型.py在

^{pr2}$

当我运行try to migrate the appp时,出现以下错误:

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初学者,请回答,提前感谢:)


Tags: djangoinfrompyimportdefaultmodelsline
2条回答

在Foreignkey和manytomy字段中使用to='<app_lable>.<Model Name>'

从文件中删除导入模型在模型中添加foreignkey和manytomy字段,就像我在下面的代码中所做的一样。to='consult.Question' 当我们从makemifration命令创建迁移时,在迁移文件中使用硬代码模型名,所以使用相同的方法编写Foreignkey和manytomy字段

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)

尝试将导入更改为: 协商中/模型.py在

import main.models.Customer

总的来说/模型.py在

^{pr2}$

然后用Customer代替main.models.Customer,用Question或{}代替{}和{}

相关问题 更多 >

    热门问题