[英]Compare Dates With SQLAlchemy One-To-Many Relationships
How do you compare dates for one-to-many SQLAlchemy relationships? 您如何比较一对多SQLAlchemy关系的日期?
Basically for my model structure, there is a one to many relationship between Team and Player: 基本上,对于我的模型结构,团队和玩家之间存在一对多的关系:
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'))
So I want to find all the recent players of a team past a certain date , but what I tried did not work: 因此, 我想查找某个日期之后某个团队的所有最近球员 ,但是我尝试的方法不起作用:
team = Team.query.filter_by(id=some_id).first()
recent_players = team.players.filter(Players.last_match.date() > some_date)
What am I doing wrong? 我究竟做错了什么?
Just recent_players = team.players.filter(Players.last_match > some_date)
should do it. 只需
recent_players = team.players.filter(Players.last_match > some_date)
。
If you really need to have only date
part of datetime
, you can cast it using sqlalchemy.sql.expression.cast : 如果确实只需要
datetime
date
部分,则可以使用sqlalchemy.sql.expression.cast进行强制转换 :
recent_players = team.players.filter(cast(Players.last_match, DATE) > some_date)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.