- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
假设我有两个 Django 应用程序:
在比赛应用程序中,我有一个代表比赛部分的模型:
class Division(models.Model):
competition = models.ForeignKey(Competition)
discipline = models.CharField(max_length=1, choices=DISCIPLINE_CHOICES)
age_group = models.ForeignKey(AgeGroup)
participants = models.ManyToManyField(Competitor, through='Entry')
我想将 Entry 模型放入条目应用程序中:
class Entry(models.Model):
division = models.ForeignKey('Division')
competitor = models.ForeignKey(Competitor)
withdrawn = models.BooleanField(default=False)
如何解决 from ... import ... 语句,使它们起作用?当我输入诸如 from entries.models import Entry
之类的导入语句时,我从这些应用程序中获取的模型被 syncdb 忽略(因为导入是循环的),或者当我删除其中一个或两个时,我得到了验证错误:
Error: One or more models did not validate: entries.entry: 'division' has a relation with model Division, which has either not been installed or is abstract. competitions.division: 'participants' specifies an m2m relation through model Entry, which has not been installed
我理解为什么会发生这种情况,但我不知道如何更改它以使其有效(无需诉诸于将 Entry 模型移动到竞赛应用程序中,我真的不想这样做)。
最佳答案
我似乎找到了答案,而且效果更稳定:)
Django documentation on the ForeignKey class说:
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.
关于python - Django:有没有办法让 ManyToManyField 中的 "through"模型与包含 ManyToManyField 的模型在不同的应用程序中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1970365/
我是一名优秀的程序员,十分优秀!