Skip to content
Advertisement

How to dynamically compose an OR query filter in Django?

From an example you can see a multiple OR query filter:

JavaScript

For example, this results in:

JavaScript

However, I want to create this query filter from a list. How to do that?

e.g. [1, 2, 3] -> Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3))

Advertisement

Answer

You could chain your queries as follows:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement