我正在尝试获取要显示的场地列表,然后计算即将举行的演出的数量。我在 处遇到了错误filtered_upcomingshows = [show for show in upcomingshows if show.start_time > current_time]。该模型将start_time字段设置为 DateTime,并current_time设置为 DateTime(除非我误解了它的使用方式?)。我无法弄清楚哪个被读取为字符串。我将如何解决这个问题? class Show(db.Model): __tablename__ = 'shows' id = db.Column(db.Integer, primary_key=True) artist_id = db.Column(db.Integer, db.ForeignKey('artists.id'), nullable = False) venue_id = db.Column(db.Integer, db.ForeignKey('venues.id'), nullable = False) start_time = db.Column(db.DateTime, nullable = False) def __repr__(self): return '<Show {} {}>'.format(self.artist_id, self.venue_id) @app.route('/venues') def venues(): current_time = datetime.now().strftime('%Y-%m-%d %H:%S:%M') venue_city_state = '' data = [] # queries Venue db for all records venues = Venue.query.all() for venue in venues: upcomingshows = venue.shows filtered_upcomingshows = [show for show in upcomingshows if show.start_time > current_time] if venue_city_state == venue.city + venue.state: data[len(data) - 1]["venues"].append({ "id": venue.id, "name": venue.name, "num_upcoming_shows": len(filtered_upcomingshows) }) else: venue_city_state == venue.city + venue.state data.append({ "city": venue.city, "state": venue.state, "venues": [{ "id": venue.id, "name": venue.name, "num_upcoming_shows": len(filtered_upcomingshows) }] })
2 回答
米琪卡哇伊
TA贡献1998条经验 获得超6个赞
问题是(正如解释器所说)这current_time
是一个字符串并且show.start_time
是一个datetime.datetime
实例。.strftime('%Y-%m-%d %H:%S:%M')
要解决此问题,您可以在定义时挂断电话current_time
。
date.strftime(format)
返回表示日期的字符串,由显式格式字符串控制。引用小时、分钟或秒的格式代码将看到 0 值。有关格式化指令的完整列表,请参阅
strftime()
和strptime()
行为。
侃侃无极
TA贡献2051条经验 获得超10个赞
current_time = datetime.now().strftime('%Y-%m-%d %H:%S:%M')
.strftime 被记录为返回一个 string,只需取消调用即可工作:
current_time = datetime.now()
添加回答
举报
0/150
提交
取消