简体   繁体   English

Django中的一对多和多对一关系

[英]One-To-Many and Many-To-One relationships in Django

I have a class Environment that has a list of Video objects (relationship One-To-Many), and in the class Video, i have a relationship with Environment of Many-To-One. 我有一个环境类,其中有一个视频对象列表(一对多关系),在视频类中,我与多对一环境有关系。

How to express this in Django? 如何在Django中表达这一点?

You need to set a foreign key in your model Video this 您需要在模型中设置外键

class Video(models.Model):
    environment = models.ForeignKey(Environment)

You need to have a look of foreignkey in django 您需要在Django中查看Foreignkey

A foreign key is a many-to one relationship. 外键是一对多的关系。 In your example, one Environment can have many videos. 在您的示例中,一个环境可以包含许多视频。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM