django中如何实现多条件查询

一:使用字典存储查询条件

    # 获取查询条件
    start_date = request.POST.get("start_date")
    end_date = request.POST.get("end_date")
    order_type = request.POST.get("order_type")
    order_nature = request.POST.get("order_nature")

    # 定义存储查询条件的字典
    search_dict = dict()

    # 如果查询条件不为空就写入到字典中
    if order_type:
        search_dict["order_type"] = order_type
    if order_nature:
        search_dict["net_self"] = order_nature

    # 此处是关键,filter过滤字典中的查询条件
    all_data = PassPicture.objects.filter(**search_dict)

    # 因为开始日期和结束日期必须都输入,才会过滤日期,所以需要此步骤
    if start_date and end_date:
        all_data = all_data.filter(Q(pass_date__gte=start_date) & Q(pass_date__lte=end_date))

    # 如果没有输入查询条件,默认返回空
    if len(all_data) == len(all_data2):
    all_data = ""

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

专职

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值