我正在嘗試獲取要顯示的場(chǎng)地列表,然后計(jì)算即將舉行的演出的數(shù)量。我在 處遇到了錯(cuò)誤filtered_upcomingshows = [show for show in upcomingshows if show.start_time > current_time]。該模型將start_time字段設(shè)置為 DateTime,并current_time設(shè)置為 DateTime(除非我誤解了它的使用方式?)。我無(wú)法弄清楚哪個(gè)被讀取為字符串。我將如何解決這個(gè)問(wèn)題? 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貢獻(xiàn)1998條經(jīng)驗(yàn) 獲得超6個(gè)贊
問(wèn)題是(正如解釋器所說(shuō))這current_time
是一個(gè)字符串并且show.start_time
是一個(gè)datetime.datetime
實(shí)例。.strftime('%Y-%m-%d %H:%S:%M')
要解決此問(wèn)題,您可以在定義時(shí)掛斷電話current_time
。
date.strftime(format)
返回表示日期的字符串,由顯式格式字符串控制。引用小時(shí)、分鐘或秒的格式代碼將看到 0 值。有關(guān)格式化指令的完整列表,請(qǐng)參閱
strftime()
和strptime()
行為。

侃侃無(wú)極
TA貢獻(xiàn)2051條經(jīng)驗(yàn) 獲得超10個(gè)贊
current_time?=?datetime.now().strftime('%Y-%m-%d?%H:%S:%M')
.strftime 被記錄為返回一個(gè) string,只需取消調(diào)用即可工作:
current_time?=?datetime.now()
添加回答
舉報(bào)
0/150
提交
取消