设计“视频”和“视频列表”表,如 Youtube 的表,使用 Django

发布于 2024-12-26 22:14:59 字数 331 浏览 4 评论 0原文

我正在使用 Django 制作一个带有“视频”和“视频列表”表的应用程序,例如 YouTube 的表。

现在,我正在制作一个“视频”和“课程(这是一个视频列表)”表。 我想将一个视频添加到一些不同的列表中,并按列表和视频进行搜索, 所以让 Course 表有 ManyToMany 字段链接到 Video 表。

但通过这种方式,我在列表中的视频顺序方面遇到了一些困难。 为了按照我注册的顺序显示视频并在之后随时修复它们,ManyToMany 字段在注册时必须有类似“id”的内容,对吧?

我是Python和Django的初学者,所以可能有一些典型的方法...... 有什么好的办法来处理这个问题吗?

I'm making an app with video" and "video list" tables, like YouTube ones, using Django.

Now, I'm making a "Video" and "Course(this is a video list)" table.
I want to add one video to some different lists and search by both list and video,
so let Course table have ManyToMany field link to Video table.

But in this way, I have some difficulty in videos' order in a list.
To display videos in the order I registered and to fix them anytime after, ManyToMany field must have something like "id" when it is registered, right?

I'm a beginner of both Python and Django, so there may be some typical way...
Any good ways to deal with this?

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

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

发布评论

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

评论(1

分开我的手 2025-01-02 22:14:59

您可以通过创建自己的中间模型(而不是使用 Django 创建的隐式模型)来完成此操作:

class Video(models.Model):
    ...

class Course(models.Model):
    ...
    videos = models.ManyToManyField(Video, through='CourseVideo', related_name='courses')

class CourseVideo(models.Model):
    class Meta:
        ordering = ['order']

    course = models.ForeignKey(Course)
    video = models.ForeignKey(Video)
    order = models.PositiveIntegerField(default=0)

上面的重要部分是 ManyToManyField 上的 through 属性。这告诉 Django 使用您定义的 CourseVideo 模型而不是它自己的隐式版本。

但是,执行此操作时会遇到一些问题。您不能再使用诸如 some_course.videos.add(video) 之类的内容,因为现在需要其他信息来创建该关系。相反,您需要显式创建关系:

CourseVideo.objects.create(course=some_course, video=some_video, order=1)

有关更多详细信息,请参阅文档: https://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany

You do this by creating your own intermediary model (instead of using the implicit one created by Django):

class Video(models.Model):
    ...

class Course(models.Model):
    ...
    videos = models.ManyToManyField(Video, through='CourseVideo', related_name='courses')

class CourseVideo(models.Model):
    class Meta:
        ordering = ['order']

    course = models.ForeignKey(Course)
    video = models.ForeignKey(Video)
    order = models.PositiveIntegerField(default=0)

The important part above is the through attribute on the ManyToManyField. This tells Django to use the CourseVideo model you defined instead of its own implicit version.

However, there's a few gotchas when you do this. You can no longer use things like some_course.videos.add(video), because additional information is needed now to create that relationship. Instead, you need to create the relationship explicitly:

CourseVideo.objects.create(course=some_course, video=some_video, order=1)

See the documentation for more details: https://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany

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