Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (47.6k points)

I'm sure this is a trivial operation, but I can't figure out how it's done.

There's got to be something smarter than this:

ids = [1, 3, 6, 7, 9] 

for id in ids:


I'm looking to get them all in one query with something like:

MyModel.objects.filter(pk=[1, 3, 6, 7, 9])

How can I filter a Django query with a list of values?

1 Answer

0 votes
by (106k points)

You can use the below mentioned piece of code from the Django documentation to filter a Django query with a list of values:

Blog.objects.filter(pk__in=[1, 4, 7])

Related questions

Browse Categories