开发者

Django queries: how to filter objects to exclude id which is in a list?

开发者 https://www.devze.com 2022-12-22 00:08 出处:网络
How can I filter in a query so the result excludes any object instances with ID belonging to a list开发者_开发技巧?

How can I filter in a query so the result excludes any object instances with ID belonging to a list开发者_开发技巧?

Lets say I have:

object_id_list = [1, 5, 345]

MyObject.objects.filter(Q(time__gte=datetime.now()) & Q( ... what to put here? ... ))

Something in the style of "SELECT * FROM ... WHERE id NOT IN (...)"


MyObject.objects.filter(time__gte=datetime.now()).exclude(id__in=object_id_list)


You can also do this using the Q object:

from django.db.models import Q

MyObject.objects.filter(time__gte=datetime.now()).filter(~Q(id__in=object_id_list))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号