如何让两个模型互相引用Django

时间:2022-06-09 08:22:08

I have the following code:

我有以下代码:

class Game(models.Model):
    title = models.CharField(max_length=50)
    summery = models.CharField(max_length=500)
    key = models.IntegerField()
    pin = models.CharField(max_length=12)
    complete = models.BooleanField()
    invite_sent = models.DateTimeField()
    on = models.ForeignKey(Member, blank = True) #<----


class Member(models.Model):
    email = models.CharField(max_length=100)
    color = models.CharField(max_length=11)
    game = models.ForeignKey(Game) #<----

The "on" foreign key links to one of the members (who's turn it is). All members of a game have their "game" foreign key set to the game they are on. The problem is that Django won't let me reference a class before it has been declared, and since I can't declare them simultaneously...

“on”外键链接到其中一个成员(轮到它了)。游戏的所有成员都将他们的“游戏”外键设置为他们所使用的游戏。问题是Django不会让我在声明它之前引用它,因为我不能同时声明它们......

Edit: To clear things up, here's an example. If there were five members playing one game, all five would have foreign keys to the game. The game on the other hand would have one foreign key to the particular member who's turn it was.

编辑:为了清理,这是一个例子。如果有五个成员玩一个游戏,那么所有五个游戏都有外键。另一方面,游戏将有一个外键给特定成员转过来。

2 个解决方案

#1


31  

The Django documentation for the ForeignKey field states:

ForeignKey字段的Django文档说明:

If you need to create a relationship on a model that has not yet been defined, you can use the name of the model, rather than the model object itself.

如果需要在尚未定义的模型上创建关系,则可以使用模型的名称,而不是模型对象本身。

So in your case, that would be:

所以在你的情况下,那将是:

class Game(models.Model):
    # Other fields...
    on = models.ForeignKey('Member', blank = True)

class Member(models.Model):
    # Other fields...
    game = models.ForeignKey(Game)

#2


8  

You don't need to have the two models reference each other with foreign keys. Remove the line:

您不需要让两个模型使用外键相互引用。删除行:

on = models.ForeignKey(Member, blank = True) #<----

and logically your Member's will still be associated to different Game's (and this makes more sense because a member can belong to one game at a time, whereas a game can have more than one member).

从逻辑上讲,你的会员仍将与不同的游戏相关联(这更有意义,因为一个成员一次可以属于一个游戏,而游戏可以有多个成员)。

You can use reverse relation to figure out which members are on a particular game.

您可以使用反向关系来确定特定游戏中的成员。

#1


31  

The Django documentation for the ForeignKey field states:

ForeignKey字段的Django文档说明:

If you need to create a relationship on a model that has not yet been defined, you can use the name of the model, rather than the model object itself.

如果需要在尚未定义的模型上创建关系,则可以使用模型的名称,而不是模型对象本身。

So in your case, that would be:

所以在你的情况下,那将是:

class Game(models.Model):
    # Other fields...
    on = models.ForeignKey('Member', blank = True)

class Member(models.Model):
    # Other fields...
    game = models.ForeignKey(Game)

#2


8  

You don't need to have the two models reference each other with foreign keys. Remove the line:

您不需要让两个模型使用外键相互引用。删除行:

on = models.ForeignKey(Member, blank = True) #<----

and logically your Member's will still be associated to different Game's (and this makes more sense because a member can belong to one game at a time, whereas a game can have more than one member).

从逻辑上讲,你的会员仍将与不同的游戏相关联(这更有意义,因为一个成员一次可以属于一个游戏,而游戏可以有多个成员)。

You can use reverse relation to figure out which members are on a particular game.

您可以使用反向关系来确定特定游戏中的成员。