首页 > 其他分享 >django models 过滤/排除指定字段的多个值

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

时间:2022-10-14 19:35:18浏览次数:46  
标签:models ip reduce django filter list 字段 import

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)))

 

标签:models,ip,reduce,django,filter,list,字段,import
From: https://www.cnblogs.com/chenjw-note/p/16792749.html

相关文章