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

Python 如何解决django中的循环导入错误?,python,django,python-3.x,django-models,django-views,Python,Django,Python 3.x,Django Models,Django Views,我知道这个错误可能是由于循环导入错误造成的,但我对它知之甚少,所以无法更正它。我尝试过使用类似问题中给出的方法,但无法解决。该项目有两个应用程序consult和main,我需要将它们的模型相互导入 请参阅/models.py from django.db import models from django.contrib.auth.models import User from main.models import Customer class Question(models.Model):

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

请参阅/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)
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)
import main.models.Customer
import consult.models.Question
import consult.models.Reply
main/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)
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)
import main.models.Customer
import consult.models.Question
import consult.models.Reply
当我运行“尝试迁移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'
回溯(最近一次呼叫最后一次):
文件“manage.py”,第22行,在
从命令行(sys.argv)执行命令
文件“C:\Users\Nikhil Khandelwal\AppData\Local\Programs\Python\Python35-
32\lib\site packages\django-1.10.4-
py3.5.egg\django\core\management\\ uuuu init\ uuuuuuu.py“,第367行,执行自_
命令行
utility.execute()
文件“C:\Users\Nikhil Khandelwal\AppData\Local\Programs\Python\Python35-
32\lib\site packages\django-1.10.4-
py3.5.egg\django\core\management\\ uuuuu init\ uuuuuuuu.py”,执行中第341行
django.setup()
文件“C:\Users\Nikhil Khandelwal\AppData\Local\Programs\Python\Python35-
32\lib\site packages\django-1.10.4-py3.5.egg\django\\uuuuu init\uuuuuu.py”,第27行,
设置中
应用程序。填充(设置。已安装的应用程序)
文件“C:\Users\Nikhil Khandelwal\AppData\Local\Programs\Python\Python35-
32\lib\site packages\django-1.10.4-py3.5.egg\django\apps\registry.py“,行
108,在
app_config.import_models(所有_models)
文件“C:\Users\Nikhil Khandelwal\AppData\Local\Programs\Python\Python35-
32\lib\site packages\django-1.10.4-py3.5.egg\django\apps\config.py“,行
199,在进口车型中
self.models\u module=导入模块(models\u module\u name)
文件“C:\Users\Nikhil Khandelwal\AppData\Local\Programs\Python\Python35-
32\lib\importlib\\ uuuuu init\uuuuuuu.py”,第126行,在导入模块中
return _bootstrap._gcd_import(名称[级别:],包,级别)
文件“”,第986行,在_gcd_import中
文件“”,第969行,在“查找”和“加载”中
文件“”,第958行,在“查找”和“加载”中解锁
文件“”,第673行,在“加载”中
exec_模块中第665行的文件“”
文件“”,第222行,在
_在已删除\u帧的情况下调用\u
文件“C:\New folder\WebD\zerovy\consult\models.py”,第3行,在
从main.models导入客户
文件“C:\New folder\WebD\zerovy\main\models.py”,第3行,在
从consult.models导入问题,回答
ImportError:无法导入名称“问题”

考虑到我是Django的初学者,请提前感谢:)

尝试将您的导入更改为以下内容: 在consult/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)
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)
import main.models.Customer
import consult.models.Question
import consult.models.Reply
在main/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)
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)
import main.models.Customer
import consult.models.Question
import consult.models.Reply
然后,您使用
main.models.Customer
而不是
Question
Reply
而不是
import consult.models.Question
consult.models.Reply
在外键和许多字段中使用
to='.

从文件中删除导入模型添加foreignkey和模型中的许多字段,就像我在下面的代码中所做的那样。
to='consult.Question'
当我们从makemifreation命令创建迁移时,在迁移文件中使用硬代码模型名,所以使用相同的方法写入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)

请添加错误的回溯。我尝试使用您的建议@omu_negrou,但出现以下错误:-我也尝试从“作为mm的主导入模型”中进行操作,但出现错误“AttributeError:module'consult.models'没有属性'Question'”尝试将
删除为mm
,然后使用完全限定的导入路径,并告诉我删除mm是否会导致错误,因为现在已经从django.db导入模型和从主导入模型导入了两个模型