Django查询:如何筛选对象以排除列表中的ID?
如何在查询中筛选,以便结果排除ID为属于列表的任何对象实例?
可以说我有:
object_id_list = [1, 5, 345] MyObject.objects.filter(Q(time__gte=datetime.now()) & Q( ... what to put here? ... ))
"SELECT * FROM ... WHERE id NOT IN (...)"
风格的东西
MyObject.objects.filter(time__gte=datetime.now()).exclude(id__in=object_id_list)
你也可以使用Q对象来做到这一点:
from django.db.models import Q MyObject.objects.filter(time__gte=datetime.now()).filter(~Q(id__in=object_id_list))