如何从类本身引用类实例

发布于 2025-01-09 02:12:05 字数 395 浏览 3 评论 0原文

我有一堂“场景”课。从这个课程中我可以有几个选择(0..n)。它们将通过一个简单的句子(字符串)呈现给场景对象的用户。每个选择都必须指向一个场景实例(next_scene)。所以我只需要将这个选择与场景实例 ID 相关联。
我如何在 Django Models 和/或 django model.Admin 中实现它。 类场景示例:

class Scene(model.Models)    
    title = models.CharField(max_length=30)
    description = models.TextField()
    choices = # TODO

我尝试了几种解决方案,但总是被阻止。我怀疑我从一开始就没有想到这一点。任何帮助将不胜感激。
史蒂芬

I have a class 'Scene'. From this class I can have several choices (0..n). They will be presented to the User of Scene object with a simple sentence (String). Each of the choices must point to a Scene instance (the next_scene). So I only need to associate this choice with a scene instance Id.
How can I implement that in Django Models, and/or in django model.Admin.
Example of a class Scene :

class Scene(model.Models)    
    title = models.CharField(max_length=30)
    description = models.TextField()
    choices = # TODO

I have tried several solutions but get always blocked. I suspect I do not conceive it right from the beginning. Any help would appreciated.
Stéphane

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

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

发布评论

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

评论(1

沫尐诺 2025-01-16 02:12:05

解决此类问题的最佳解决方案是ManyToMany 关系。您可以将其设置为相同的模型:

class Scene(models.Model):
    ...
    choices = models.ManyToManyField("self", blank=True)

The best solution for such thing is ManyToMany relation. You can set it to the same model:

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