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?
You do this by creating your own intermediary model (instead of using the implicit one created by Django):
The important part above is the
throughattribute on theManyToManyField. This tells Django to use theCourseVideomodel 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:See the documentation for more details: https://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany