django models 过滤/排除指定字段的多个值

from operator import or_
from django.db.models import Q
from functools import reduce

filter_ip_list = ["不能为空"]

# 排除
models.ServerHost.objects.filter(b_project_id=99).exclude(reduce(or_, (Q(host_ip=filter_ip) for filter_ip in filter_ip_list)))

# 过滤
models.ServerHost.objects.filter(b_project_id=99).filter(reduce(or_, (Q(host_ip=filter_ip) for filter_ip in filter_ip_list)))

 

posted @ 2022-10-14 19:18  chenjianwen  阅读(457)  评论(0编辑  收藏  举报