SQLAlchema,Python我想按从开始日期到结束日期的创建日期过滤模型

时间:2019-02-19 08:39:46

标签: python sqlalchemy

我有一个模型,我想通过用户输入DateTime来过滤数据,该DateTime从一天的开始到一天的结束,例如DateTime从DateTime“ 2018-10-18T00:59:59.711602”到DateTime“ 2018- 10-18T23:59:59.711602“

   @classmethod
   def filter_by_created_date(cls, query, **kwargs):
        getDate = kwargs.get("filter_by_created_date")
        getSecondArgsDate = kwargs.get("filter_by_created_date_second_args")

        if  getDate and getSecondArgsDate is not None:
            DBMediaItem = GrapheneType.db_cls("MediaItem")

            query = query.filter(
                DBMediaItem.created_at < getDate,
                DBMediaItem.created_at >= getSecondArgsDate
            )

        return query

1 个答案:

答案 0 :(得分:1)

介于:

query = query.filter(DBMediaItem.created_at.between(getDate, getSecondArgsDate))