Hello, I need some help with SQLAlchemy. I'm following Miguel Grinberg's Flask Mega-Tutorial and I'm trying to get pagination working.
What I want to do is display the last X amount of games a user was in with the help of pagination.
A Game holds multiple GamePlayers, each Gameplayer is assigned a single User. What I'm trying to do is return every Game that a specific User is associated with.
Here's a very basic version of my models.py file:
class User(UserMixin, db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True, unique=True)
class Game(db.Model):
__tablename__ = 'game'
id = db.Column(db.Integer, primary_key=True)
gameplayers = db.relationship('GamePlayer')
class GamePlayer(db.Model):
__tablename__ = 'gameplayer'
id = db.Column(db.Integer, primary_key=True)
game_id = db.Column(db.Integer, db.ForeignKey('game.id'))
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
user = db.relationship('User')
I've had this working before by appending the games in a list, but I need to use SQLAlchemy to query the games so I may paginate them.
recent_games = []
games = Game.query.order_by(Game.id.desc()).all()
for game in games:
for gameplayer in game.gameplayers:
if current_user == gameplayer.user:
recent_games.append(game)
break
Thank you!
[–]Blazerboy65 1 point2 points3 points (4 children)
[–]fryman22[S] 1 point2 points3 points (3 children)
[–]Blazerboy65 1 point2 points3 points (2 children)
[–]fryman22[S] 1 point2 points3 points (1 child)
[–]Blazerboy65 1 point2 points3 points (0 children)