I'm trying to set up manytomany relationship through an intermediary table with extra columns in Peewee 3.2.2 like this:
ThroughDeferred = DeferredThroughModel()
class Playlist(BaseModel):
...
movies = ManyToManyField(Movie, backref='playlists', through_model=ThroughDeferred)
class Movie(BaseModel):
name = CharField(max_length=100)
class PlaylistMovie(BaseModel):
playlist = ForeignKeyField(column_name='playlist_id', field='id', model=Playlist)
movie = ForeignKeyField(column_name='movie_id', field='id', model=Movie)
position = PositiveSmallIntegerField(default=1)
class Meta:
table_name = 'playlist_movie_relation'
ThroughDeferred.set_model(PlaylistMovie)
But then, upon query all i get is the list of related movie data without the position.
list(playlist.movies.dicts())
> [{name: 'blah', id: 3}, ...]
How can I get the position data within the playlist.movies?
This ought to work:
query = (Movie
.select(Movie, PlaylistMovie.position)
.join(PlaylistMovie)
.where(PlaylistMovie.playlist == playlist)
.order_by(PlaylistMovie.position))
for movie in query:
print(movie.name, movie.playlistmovie.position)