加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – Django:有没有办法在与包含ManyToManyField的模型不

发布时间:2020-12-16 23:24:59 所属栏目:Python 来源:网络整理
导读:可以说我有两个 django应用程序: 比赛 – 将处理比赛数据 条目 – 将处理与参赛者进入比赛相关的功能 在比赛应用程序中,我有一个模型代表比赛的一部分: class Division(models.Model): competition = models.ForeignKey(Competition) discipline = models.
可以说我有两个 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读