python – Django:有没有办法在与包含ManyToManyField的模型不同的应用程序中的ManyToMa

可以说我有两个
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 …语句,以便它们有效?当我输入import语句时,例如来自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.

dawei

【声明】:丽水站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。