Use*_*er0 15 python django django-models
好吧我正在尝试编写组织良好的代码并实际制作单独的django应用程序,而不是将所有内容整合到1中.我的问题是我有3个应用程序,每个应用程序从下一个应用程序引用另一个模型.所以基本上我有一个无限循环,App A需要知道B.models.something1,App B需要知道C.models.Somthing2,而App C需要知道A.models.something3.这当然不会运行,对于那些想知道这实际上是否有问题的人:).有什么类似于类的预先声明,所以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
Run Code Online (Sandbox Code Playgroud)
学生/ models.py
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)
Run Code Online (Sandbox Code Playgroud)
学校/ 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
Run Code Online (Sandbox Code Playgroud)
所以这就是我得到的:
文件"/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号线,从school.models导入学校文件"/Users/userzero/django/myapp/casino/models.py ",第2行,来自teacher.models导入位置ImportError:无法导入名称位置
Mic*_*unn 37
来自文档:
要引用另一个应用程序中定义的模型,您可以使用完整的应用程序标签显式指定模型.例如,如果上面的制造商模型在另一个名为production的应用程序中定义,则需要使用:
class Car(models.Model):
manufacturer = models.ForeignKey('production.Manufacturer')
Run Code Online (Sandbox Code Playgroud)
在解析两个应用程序之间的循环导入依赖关系时,此类引用非常有用.
因此,对于您的应用,请尝试更改例如
location = models.ForeignKey(Location)
Run Code Online (Sandbox Code Playgroud)
至
location = models.ForeignKey('Location')
Run Code Online (Sandbox Code Playgroud)
请注意,如果此模型位于不同的应用程序中,那么您也需要指定它(感谢@Bran指出这一点),例如
location = models.ForeignKey('teacher.Location')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5757 次 |
| 最近记录: |