2011-01-27 55 views
15

好吧所以我正在嘗試編寫很好的組織代碼,實際上製作單獨的Django應用程序,而不是將所有內容合併到1中。我的問題是我有3個應用程序,每個應用程序引用另外1個模型下一個應用。所以基本上我有一個無限循環,應用程序A需要知道關於B.models.something1,應用程序B需要知道關於C.models.Somthing2,而應用程序C需要知道關於A.models.something3。這當然不會運行,因爲那些想知道這是否實際上是一個問題:)。是否有任何類似於類的預先聲明,所以Python會知道這些類實際上存在?Django/Python循環模型參考

謝謝。

編輯:更多代碼: 不幸的是,我的項目的性質和模型是保密的,所以我必須更改名稱以反映完全不同的內容,但代碼將保持不變。

老師/ models.py

from django.db import models 
from myapp.student.models import * 
from django.contrib.auth.models import User 
class Teacher(models.Model): 
    """(description)""" 
    user = models.ForeignKey(User) 
    name = models.CharField(max_length=100) 
    phone = models.CharField(max_length=13) 
    phone_ext = models.CharField(blank=True, max_length=5) 
    fax = models.CharField(blank=True, max_length=13) 
    fax_ext = models.CharField(blank=True, max_length=100) 
    url = models.URLField(blank=True, verify_exists=True) 
    complaint = models.ManyToManyField(Complaint) 
    city = models.CharField(blank=True, max_length=100) 
    state = models.CharField(blank=True, max_length=100) 
    postal_code = models.CharField(blank=True, max_length=15) 
    location = models.ManyToManyField(Location) 
    def __unicode__(self): 
     return self.name 
class Location(models.Model): 
    """(description)""" 
    city = models.CharField(blank=True, max_length=100) 
    state = models.CharField(blank=True, max_length=100) 
    country = models.CharField(blank=False, max_length=100) 
    def __unicode__(self): 
     return self.city + ", " + self.state +", "+self.country 

學生/ models.py

​​

學校/ models.py

from django.db import models 
from myapp.teacher.models import Location 
class School(models.Model): 
    """(School description)""" 
    name = models.CharField(max_length=100) 
    url = models.URLField(verify_exists=True) 
    img = models.ImageField(upload_to="casion_img/") 
    rating = models.FloatField() 
    description = models.CharField(blank=True, max_length=300) 
    goodstanding = models.BooleanField(default=True) 
    location = models.ForeignKey(Location) 
    def __unicode__(self): 
     return self.name 

所以這裏就是我得到:

文件「/ Users/userzero/dj ango/myapp/school/models.py「,第2行,來自teacher.models的 導入位置 文件」/Users/userzero/django/myapp/teacher/models.py「,第2行,來自student.models的 進口投訴 文件「/Users/userzero/django/myapp/student/models.py」,3號線,在 從school.models導入學校 文件「/Users/userzero/django/myapp/casino/models.py」 ,2號線,在 從teacher.models導入位置 導入錯誤:無法導入名稱位置

+0

導入週期不一定是問題。但沒有更多的代碼,我們無法弄清楚它到底是什麼。 – 2011-01-27 06:18:55

回答

37

docs

To refer to models defined in another application, you can explicitly specify a model with the full application label. For example, if the Manufacturer model above is defined in another application called production, you'd need to use:

class Car(models.Model): 
    manufacturer = models.ForeignKey('production.Manufacturer') 

This sort of reference can be useful when resolving circular import dependencies between two applications.

因此,對於你的應用程序,嘗試改變如

location = models.ForeignKey(Location) 

location = models.ForeignKey('Location') 

需要注意的是,如果這種模式是在不同的應用程序,然後你需要指定太(感謝@Bran指出了這一點),例如

location = models.ForeignKey('teacher.Location') 
+0

我不知道PyCheckMate,但是...如果你的Django應用程序工作,並且修復程序是有記錄的Django技術,那麼我的猜測是在PyCheckMate中出現故障 – 2011-01-27 08:19:09