Django筛选器查询条件更改

声明:此方法不是最佳方案,甚至可能是最笨的方法,只是在项目过程中最快想到的方法,如果有更方便方法,非常欢迎留言

 

问题描述:客户希望提供自定义的查询日期格式,但在设计数据库的时候没有按照那种格式,如果直接使用DataTimeFilter筛选就会报日期格式错误

解决方法:

在请求进入的时候,将request.GET更改为可修改状态,然后再格式化查询条件

# 请求格式'20191229102134'
# 目标格式'xxxx-xx-xx xx:xx:xx.xxxxxx'
def get(self, request, *args, **kwargs):
        request.GET._mutable = True
        if 'start_time' in request.GET:
            start_time = request.GET['start_time']
            request.GET['start_time'] = start_time[:4] + '-' + start_time[4:6] + '-' + start_time[6:8] + ' ' + \
                                           start_time[8:10] + ':' + start_time[10:12] + ':' + start_time[12:] + \
                                           '.000000'
        if 'end_time' in request.GET:
            end_time = request.GET['end_time']
            request.GET['end_time'] = end_time[:4] + '-' + end_time[4:6] + '-' + end_time[6:8] + ' ' + \
                                    end_time[8:10] + ':' + end_time[10:12] + ':' + end_time[12:] + '.999999'

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值