2017-06-17 60 views
3

我知道錯誤可能是由於循環導入錯誤,但沒有太多的知識,我無法糾正它。我已經使用了類似的問題給出的方法試過,但未能解決後援項目有兩個應用諮詢及主,我需要他們的模型導入到對方,如何解決django中的循環導入錯誤?

諮詢/ 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) 

主/ 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) 

當我運行嘗試遷移appps以下錯誤出現:

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初學者,請多關照:)

+0

請添加錯誤的回溯。 – WPedrak

+0

[http://dpaste.com/2FBKXMC](http://dpaste.com/2FBKXMC) –

回答

2

使用to='<app_lable>.<Model Name>'在FOREIGNKEY和多對多字段

卸下進口車型從文件添加外鍵和多對多領域模型像我下面的代碼做英寸to='consult.Question' 當我們創建,在遷移文件中使用硬編碼的型號名稱makemifration命令遷移,所以用同樣的方式來寫外鍵和多對多場

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) 
+0

謝謝,它的工作就像我想要的一樣至。 –

2

試着改變你的進口這樣的:在諮詢/ models.py

import main.models.Customer 
在主/ models.py

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

然後,而不是Customer您使用main.models.Customer取而代之的QuestionReply您使用import consult.models.Questionconsult.models.Reply

+0

我試着用你的建議@omu_negru,但得到了以下錯誤: - [http://dpaste.com/1MZ9N6R]( http://dpaste.com/1MZ9N6R) –

+0

我也嘗試從「主要導入模型爲毫米」,但它給了錯誤「AttributeError:模塊'consult.models'沒有屬性'問題'」 –

+0

嘗試刪除'因爲毫米'然後使用完全合格的導入路徑,並告訴我它是否工作 –

相關問題