Django OneToOneField 结构
我一直在研究和浏览所有文档,但我仍然有点困惑,我想可能是因为有多种方法可以使用 OnToOneField。
我有 4 个模型:赛前、赛中、赛后和比赛。我希望“游戏”包含其他三个模型。
截至目前,它看起来像这样......
class Pregame(models.Model):
game_id = models.CharField(max_length=10)
other fields...
def __str__(self):
return str(self.game_id)
class Ingame(models.Model):
game_id = models.CharField(max_length=10)
other fields...
def __str__(self):
return str(self.game_id)
class Postgame(models.Model):
game_id = models.CharField(max_length=10)
other fields...
def __str__(self):
return str(self.game_id)
class Game(models.Model):
game_id = models.CharField(max_length=10)
pregame = models.OneToOneField(
Pregame, on_delete=models.CASCADE, null=True)
ingame = models.OneToOneField(
Ingame, on_delete=models.CASCADE, null=True)
postgame = models.OneToOneField(
Postgame, on_delete=models.CASCADE, null=True)
def __str__(self):
return str(self.game_id)
我使用 OnToOne 因为每个游戏只有一个 Pregame、Ingame 和 Postgame,而其他三个模型只属于一个 Game。
我有几个问题我很困惑。
如果其他模型对象之一尚不存在,我是否能够拥有 Game 对象?就像如果有 Pregame 但没有 Ingame 或 Postgame,Game 是否还会存在而只有 Pregame 呢?我看过几个视频,他们做了默认='{}',这是我应该做的吗?
每个模型都有 game_id,这就是我将它们连接到“Game”对象中的方式。是否有像 game_id=game_id 这样的 OneToOneField 选项,以便“游戏”模型会自动将所有模型链接在一起,或者我仍然需要单独进行此操作?
感谢您的帮助和知识。
I have been researching and looking through all the docs but I am still a bit confused, I think maybe because there are multiple ways to use the OnToOneField.
I have 4 models, Pregame, Ingame, Postgame and Game. And I want 'Game' to contain the other three models.
As of right now it looks like this...
class Pregame(models.Model):
game_id = models.CharField(max_length=10)
other fields...
def __str__(self):
return str(self.game_id)
class Ingame(models.Model):
game_id = models.CharField(max_length=10)
other fields...
def __str__(self):
return str(self.game_id)
class Postgame(models.Model):
game_id = models.CharField(max_length=10)
other fields...
def __str__(self):
return str(self.game_id)
class Game(models.Model):
game_id = models.CharField(max_length=10)
pregame = models.OneToOneField(
Pregame, on_delete=models.CASCADE, null=True)
ingame = models.OneToOneField(
Ingame, on_delete=models.CASCADE, null=True)
postgame = models.OneToOneField(
Postgame, on_delete=models.CASCADE, null=True)
def __str__(self):
return str(self.game_id)
I am using OnToOne because each Game will only have one Pregame, Ingame and Postgame and the other three models will only belong to one Game.
I have a couple questions I am confused about.
Will I be able to have a Game object if one of the other model objects doesn't exist yet? Like if there is a Pregame but no Ingame or Postgame, will Game still exist with just Pregame inside of it? I seen a couple videos where they did a default='{}', is that what I should be doing?
Each model has game_id and that is how I am connecting them all together into the'Game' object. Is there a OneToOneField option like game_id=game_id so the 'Game' model will automatically link all the models together or would I still have to do that separate?
Thank you for your help and knowledge.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您不必在 *game 实体上设置
game_id
。您只需添加related_name
:是的,即使没有 *game,
Game
对象也可以存在,正如您添加的null=True
一样。当您的模型不接受 null/空值时,您应该使用默认值。创建此类模型后,您就可以通过
Game
实体进行访问。例如,从赛前
到赛后
:First of all, you don't have to set
game_id
on the *game entities. You can just addrelated_name
:Yes, a
Game
object can exist even without a *game, as you addednull=True
. You should use a default when your model does not accept null/empty values.Once you create such a model, you have access via the
Game
entity. For example from going topregame
topostgame
: