检索播放列表中的所有歌曲多对多数据库

时间:2016-12-06 06:06:21

标签: python web2py web2py-modules

我有播放列表和曲目的数据库结构。

我的数据库代码如下所示:

 #table for tracks
 db.define_table('track',
            Field('artist'),
            Field('album'),
            Field('title'),
            Field('duration', 'float'),
            ......
            )
#table for playlists
db.define_table('playlist',
            Field('title')
            )
#table for references
db.define_table('playlist_reference',
            Field('playlist', 'reference playlist'),
            Field('track','reference track')
            )

我创建了一个控制器,通过访问这些表来检索特定播放列表中的所有曲目。这是代码的样子:

def get_playlist_tracks():
    title = request.vars.title
    tracks = []

    q = (title == db.playlist.title)

    #searches the playlist database for the playlist that 
    #matches the title of the playlist whose tracks I want to retrieve
    #and gives me its id
    plist_id = db(q).select().first().id

    #returns all the references in which that playlist appears
    q = (plist_id == db.playlist_reference.playlist)
    refs = db(q).select(db.playlist_reference.ALL)

    #for each reference I get the track id and append to tracks array
    for i, r in enumerate(refs):
        t_id = r.track
        q = (t_id == db.track.id)
        track = db(q).select(db.track.ALL)
        print track
        tracks.append(track)

    return response.json(dict(
        tracks=tracks)
    )

出于某种原因,此代码不会返回我期望的内容,即该播放列表中的曲目列表。

可能是什么问题?

谢谢

0 个答案:

没有答案