Here is the query I use to get the data:
q = db.query(Stat, Song, Commit).join(Song).join(Commit)
Well, I actually use something like db.query(Stat.rating, Stat.play_count, Song.path, ...
.
Now I can filter the results like this:
if user:
q = q.filter(Commit.user == user)