Home > OS >  Django database query: how to get multiple objects by id?
Django database query: how to get multiple objects by id?

Time:05-29

i want to get a filtered object with several id's that i will specify

TestQuestionBlok.objects.filter()

How to write this filter?

CodePudding user response:

If you have a list of the ids, like [1, 4, 9], you can work with the __in lookup [Django-doc]:

TestQuestionBlok.objects.filter(pk__in=[1, 4, 9])

Given these ids exist (in the database), these will be in the queryset. So it will return at most three items here with the same query.

CodePudding user response:

you can use __in in your filter Condition and set list value Example:

TestQuestionBlok.objects.filter(id__in=[1,8,9])
  • Related