Django 中两个模型之间的关系

发布于 2025-01-14 08:44:04 字数 113 浏览 2 评论 0原文

这似乎是一个微不足道的问题,但我对 Django 很陌生。我有两个模型用户和竞争。

用户可以创建并参加一项或多项比赛。 当用户可以是竞赛的所有者并且可以是一个或多个竞赛的参与者时,如何指定这种关系。

It seems like a trivial question but I am new to Django. I have two models user and competition.

The user can create and join one or many competition.
How to specify this relation as the user can be the owner of competition/s and can be a participant in one or more competition.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岛歌少女 2025-01-21 08:44:05

我假设您有两个表 UserCompetition

然后在竞争中您可以使用 models.ManyToManyField

示例

class User:
   ...

class Competition:
   ...
   creator = models.ForeignKey(User)
   participents = models.ManyToManyField(User)

I Assume You have two tables User and Competition:

then in competition you can user models.ManyToManyField

Example

class User:
   ...

class Competition:
   ...
   creator = models.ForeignKey(User)
   participents = models.ManyToManyField(User)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文