How can i perform 'row in' condition in django

Viewed 49

I am using django, i want to implement a condition evaluating specific fields in a subquery, for instance:

SELECT a, b, c FROM letters
   WHERE row(a,b) IN (
      SELECT a, b FROM other_letters
   )

can i do a row filter in django?

1 Answers

I don't think this is currently possible as of Django 3.1.

You will have to use multiple subqueries if you want to use the ORM:

a = OtherLetters.objects.all().values('a')
b = OtherLetters.objects.all().values('b')
letters = Letters.objects.filter(a__in=a, b__in=b).values('a', 'b', 'c')

This is roughly equivalent to the following:

SELECT a, b, c FROM Letters 
WHERE (
    a IN (SELECT a FROM OtherLetters) AND 
    b IN (SELECT b FROM OtherLetters)
) 
Related