Django对不确定多条件进行求交集搜索

使用Django的Q方法创建搜索条件:

复制代码
name = request.GET.get('name')
            pages = request.GET.get('pages')
            operator = request.GET.get('operator')
            date1 = request.GET.get('date1')
            date2 = request.GET.get('date2')
            print('date1', type(date1), date2)
            conditions = []
            if name:
                conditions.append(Q(account__contains=name))
            if pages:
                conditions.append(Q(model_name=pages))
            if operator:
                conditions.append(Q(operation=operator))
            if date1:
                date1 = date1 + ' 00:00:00'
                conditions.append(Q(created_at__gte=date1))
            if date2:
                date2 = date2 + ' 23:59:59'
                conditions.append(Q(created_at__lte=date2))
            if conditions:
                intersection_query = Q(*conditions)
                res_info = OperationLog.objects.filter(intersection_query).order_by('-created_at')
复制代码

 

posted @   lytcreate  阅读(39)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· 地球OL攻略 —— 某应届生求职总结
点击右上角即可分享
微信分享提示