如何比较一对多SQLAlchemy关系的日期?
基本上对于我的模型结构,团队和玩家之间存在一对多的关系:
class Team(db.Model):
__tablename__ = 'Team'
id = db.Column(db.Integer, primary_key=True)
players = db.relationship('Player', backref='team', lazy='dynamic')
class Player(db.Model):
__tablename__ = 'Player'
id = db.Column(db.Integer, primary_key=True)
last_match = db.Column(db.DateTime)
team_id = db.Column(db.Integer, db.ForeignKey('Team.id'))
所以我想找到一个团队过去某个日期的所有最近的球员,但我尝试的不起作用:
team = Team.query.filter_by(id=some_id).first()
recent_players = team.players.filter(Players.last_match.date() > some_date)
我做错了什么?
答案 0 :(得分:0)
只需recent_players = team.players.filter(Players.last_match > some_date)
即可。date
datetime
部分,则可以使用sqlalchemy.sql.expression.cast进行转换:
recent_players = team.players.filter(cast(Players.last_match, DATE) > some_date)