简体   繁体   中英

How to exclude a list of numbers from a QuerySet?

I have a list of integers:

numbers = [1, 2, 3, 4]

I have a model, MyModel, with primary keys ranging 1 to 1000

I would like to exclude the models with primary keys in the list 'numbers' from MyModel during a QuerySet:

MyModel.objects.exclude(pk in numbers)

The above doesn't work.

What's the best way to do so?

您可以使用in过滤器查询列表中的字段

MyModel.objects.exclude(pk__in=numbers)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM