一:使用字典存储查询条件
# 获取查询条件
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 = ""