最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Flask中对于SQLAlachemy中检索特定时间内的条目

Flask crifan 4283浏览 0评论

现在已有对应的一个活动的列表

现在需要query时去filter检索出:

时间是当天的,明天的,一周内的,一周之外的

SQLAlchemy(二) — ORM – 作业部落 Cmd Markdown 编辑阅读器

sqlalchemy datetime filter

python – Doing DateTime Comparisons in Filter SQLAlchemy – Stack Overflow

python – sqlalchemy filter by comparing datetime.now() and column default date – Stack Overflow

SQLAlchemy: filter by date for an datetime field(does not work with SQLite, with PostgreSQL it works fine)

sqlalchemy datetime filter between

SQLAlchemy ORM Examples | Python Central

python – SQLAlchemy query filter between two times slow when time period is 30 minutes – Stack Overflow

sqlalchemy datetime today

datetime – Make Sqlalchemy Use Date In Filter Using Postgresql – Stack Overflow

python – SQLAlchemy default DateTime – Stack Overflow

Column Insert/Update Defaults — SQLAlchemy 1.1 Documentation

sqlalchemy filter by multiple

python – sqlalchemy filter multiple columns – Stack Overflow

[已解决]Python中判断datetime是否是当天

[已解决]SQLAlchemy中filer_by中一次传入多个条件

至此,终于解决了:

通过:

    dateFormat = “%Y-%m-%d”
    timeFormat = “%H:%M:%S”
    datetimeFormat = dateFormat + ” ” + timeFormat
    app.logger.debug(‘dateFormat=%s, timeFormat=%s, datetimeFormat=%s’, dateFormat, timeFormat, datetimeFormat)
    todayDatetime = datetime.today()
    todayDate = date.today()
    #todayDateStr = todayDatetime.strftime(dateFormat)
    todayDateStr = todayDate.strftime(dateFormat)
    app.logger.debug(‘todayDate=%s, todayDateStr=%s’, todayDate, todayDateStr)
    todayStartStr = todayDateStr + ” 00:00:00″
    todayEndStr = todayDateStr + ” 23:59:59″
    app.logger.debug(‘todayStartStr=%s, todayEndStr=%s’, todayStartStr, todayEndStr)
    todayStart = datetime.strptime(todayStartStr, datetimeFormat)
    todayEnd   = datetime.strptime(todayEndStr, datetimeFormat)
    app.logger.debug(‘todayStart=%s, todayEnd=%s’, todayStart, todayEnd)
    #todayEventList = Event.query.filter_by(and_(user_openid=curUser.openid, start_date.date() = dateToday)).all()
    #todayEventList = Event.query.filter_by(and_(user_openid=curUser.openid, start_date >= todayStart, start_date <= todayEnd)).all()
    #todayEventList = Event.query.filter_by(and_(user_openid == curUser.openid, start_date >= todayStart, start_date <= todayEnd)).all()
    #todayEventList = Event.query.filter_by(user_openid=curUser.openid, start_date >= todayStart, start_date <= todayEnd).all()
    #todayEventList = Event.query.filter_by(and_(Event.user_openid == curUser.openid, Event.start_date >= todayStart, Event.start_date <= todayEnd)).all()
    todayEventList = Event.query.filter(and_(Event.user_openid == curUser.openid, Event.start_date >= todayStart, Event.start_date <= todayEnd)).all()
    app.logger.debug(“todayEventList=%s”, todayEventList)

去过滤出,属于当天这个时间范围内的内容:

dateFormat=%Y-%m-%d, timeFormat=%H:%M:%S, datetimeFormat=%Y-%m-%d %H:%M:%S

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in views [/root/html/SIPEvents/sipevents/views.py:385]:
todayDate=2016-08-29, todayDateStr=2016-08-29

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in views [/root/html/SIPEvents/sipevents/views.py:388]:
todayStartStr=2016-08-29 00:00:00, todayEndStr=2016-08-29 23:59:59

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in views [/root/html/SIPEvents/sipevents/views.py:391]:
todayStart=2016-08-29 00:00:00, todayEnd=2016-08-29 23:59:59

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in views [/root/html/SIPEvents/sipevents/views.py:399]:
todayEventList=[<Event id=5 user_openid=oswjmv4X0cCXcfkIwjoDfCkeTVVY title=u’\u4eca\u5929\u505a\u5f00\u53d1′>]

转载请注明:在路上 » [已解决]Flask中对于SQLAlachemy中检索特定时间内的条目

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.195 seconds, using 22.32MB memory