2011-01-27 54 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