Django/Python循环模型参考
好的,所以我试着编写很好的有组织的代码,并实际制作单独的django应用程序,而不是将所有内容都集中到一个应用程序中。我的问题是,我有3个应用程序,每个应用程序引用下一个应用程序中的另一个模型。所以基本上我有一个无休止的循环,应用程序A需要了解B.models.something 1,应用程序B需要了解C.models.somthing 2,应用程序C需要了解A.models.something 3。对于那些想知道这是否真的是个问题的人来说,这当然不会运行:)。是否有类似于类的预声明的东西,以便python知道类实际上存在 谢谢 编辑:更多代码: 不幸的是,我的项目的性质和模型是保密的,所以我必须更改名称以反映完全不同的内容,但代码将保持不变 教师/模特.pyDjango/Python循环模型参考,python,django,django-models,Python,Django,Django Models,好的,所以我试着编写很好的有组织的代码,并实际制作单独的django应用程序,而不是将所有内容都集中到一个应用程序中。我的问题是,我有3个应用程序,每个应用程序引用下一个应用程序中的另一个模型。所以基本上我有一个无休止的循环,应用程序A需要了解B.models.something 1,应用程序B需要了解C.models.somthing 2,应用程序C需要了解A.models.something 3。对于那些想知道这是否真的是个问题的人来说,这当然不会运行:)。是否有类似于类的预声明的东西,以便
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
from django.db import models
from django.contrib.auth.models import User
from myapp.school.models import School
class Student(models.Model):
"""(Student description)"""
user = models.ForeignKey(User)
country = models.CharField(max_length=100)
state = models.CharField(max_length=100)
city = models.CharField(max_length=100)
locale = models.CharField(blank=False, max_length=5)
learningtype = models.CharField(blank=True, max_length=100)
sites = models.TextField(blank=True)
def __unicode__(self):
return str(self.user)
class Complaint(models.Model):
"""(Complaint description)"""
student = models.ForeignKey(Student)
site = models.ForeignKey(School)
complaint = models.TextField(blank=False)
def __unicode__(self):
return str(self.site)
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
学生/模特.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
from django.db import models
from django.contrib.auth.models import User
from myapp.school.models import School
class Student(models.Model):
"""(Student description)"""
user = models.ForeignKey(User)
country = models.CharField(max_length=100)
state = models.CharField(max_length=100)
city = models.CharField(max_length=100)
locale = models.CharField(blank=False, max_length=5)
learningtype = models.CharField(blank=True, max_length=100)
sites = models.TextField(blank=True)
def __unicode__(self):
return str(self.user)
class Complaint(models.Model):
"""(Complaint description)"""
student = models.ForeignKey(Student)
site = models.ForeignKey(School)
complaint = models.TextField(blank=False)
def __unicode__(self):
return str(self.site)
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
学校/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
from django.db import models
from django.contrib.auth.models import User
from myapp.school.models import School
class Student(models.Model):
"""(Student description)"""
user = models.ForeignKey(User)
country = models.CharField(max_length=100)
state = models.CharField(max_length=100)
city = models.CharField(max_length=100)
locale = models.CharField(blank=False, max_length=5)
learningtype = models.CharField(blank=True, max_length=100)
sites = models.TextField(blank=True)
def __unicode__(self):
return str(self.user)
class Complaint(models.Model):
"""(Complaint description)"""
student = models.ForeignKey(Student)
site = models.ForeignKey(School)
complaint = models.TextField(blank=False)
def __unicode__(self):
return str(self.site)
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/django/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行,在
从学校。模型导入学校
文件“/Users/userzero/django/myapp/casino/models.py”,第2行,在
从teacher.models导入位置
导入错误:无法从以下位置导入名称位置:
参考另一个文件中定义的模型
应用程序,您可以显式地
指定一个具有完整属性的模型
应用程序标签。例如,如果
上述制造商型号定义见
另一个应用叫做生产,
您需要使用:
这类参考资料可能很有用
解析循环导入时
两个应用程序之间的依赖关系
因此,对于您的应用程序,请尝试更改,例如
location = models.ForeignKey(Location)
到
请注意,如果此型号在不同的应用程序中,那么您也需要指定该型号(感谢@Bran指出这一点),例如
导入周期不一定是个问题。但是如果没有更多的代码,我们就无法理解它到底是什么。我不知道PyCheckMate,但是。。。如果你的Django应用程序可以工作,并且修复程序是一个有文档记录的Django技术,那么我猜是PyCheckmate中的错误,结果证明这不起作用。syncb工作的原因显然是我的设置文件中的应用程序被删除了(考虑到我没有编辑它,而且我是项目中唯一的一个,我也不知道这是怎么发生的)。这是一个打击!根据您对问题的描述,模型字符串应该可以工作。如果没有,肯定还有别的事情发生。你能试着从三个应用中的两个删除所有内容,然后一点一点地添加回来,直到抛出异常为止吗。您的错误消息是否提供了其他线索?我在尝试向管理员网站注册模型时遇到过类似于您的错误(管理员在模型准备好验证之前尝试验证模型)。您必须包含应用程序名称,而不是
models.ForeignKey('Location')
您将使用models.ForeignKey('teacher.Location')