Python “TypeError:“>”在“datetime.datetime”和“str”的实例之间不受支持”

Python “TypeError:“>”在“datetime.datetime”和“str”的实例之间不受支持”,python,postgresql,flask,sqlalchemy,Python,Postgresql,Flask,Sqlalchemy,我试图得到一份展示场地的清单,然后数一数即将到来的表演的数量。我在filtered_upcomingshows=[show.start_time>current_time时在upcomingshows中为show显示时遇到错误。该模型的“开始时间”字段设置为DateTime,而“当前时间”设置为DateTime,除非我误解了它的用法?。我不知道哪一个被读作字符串。我该如何解决这个问题 class Show(db.Model): __tablename__ = 'shows'

我试图得到一份展示场地的清单,然后数一数即将到来的表演的数量。我在filtered_upcomingshows=[show.start_time>current_time时在upcomingshows中为show显示时遇到错误。该模型的“开始时间”字段设置为DateTime,而“当前时间”设置为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)
            }]
          })

问题在于解释器说当前_时间是字符串,而show.start_时间是datetime.datetime实例。若要解决此问题,您可以在定义当前\u时间时删除.strftime“%Y-%m-%d%H:%S:%m”调用

date.strftimeformat

返回表示日期的字符串,由显式格式字符串控制。表示小时、分钟或秒的格式代码将显示0值。有关格式化指令的完整列表,请参阅strftime和strptime行为


参考:

问题在于解释器说当前时间是字符串,而show.start\u时间是datetime.datetime实例。若要解决此问题,您可以在定义当前\u时间时删除.strftime“%Y-%m-%d%H:%S:%m”调用

date.strftimeformat

返回表示日期的字符串,由显式格式字符串控制。表示小时、分钟或秒的格式代码将显示0值。有关格式化指令的完整列表,请参阅strftime和strptime行为

参考:

.strftime被记录为,只需取消呼叫即可:

current_time = datetime.now()
.strftime被记录为,只需取消呼叫即可:

current_time = datetime.now()

啊,我引用了另一段代码,并假设所有这些都是格式化它或其他东西所必需的。很高兴知道,谢谢!啊,我引用了另一段代码,并假设所有这些都是格式化它或其他东西所必需的。很高兴知道,谢谢!