[Fixed]-Django easy retrieval of items in a Many-to-many relationship model

1👍

You can use a through table:

class Playlist(models.Model):
    videos = models.ManyToManyField("Video", through="PlaylistVideos")

This is documented here. You can still access the videos as playlist.videos but it will generate multiple queries and will thus be less efficient than before. This is discussed in How do I make Django ManyToMany 'through' queries more efficient?. You will need to decide if the extra queries will significantly impact your application. As discussed in the docs, you will not be able to add new videos to a playlist without using the PlaylistVideos model, since you will need to specify a value for new_field.

Leave a comment