Reputation: 642
Problem Statement: Make following query in Django API.
Table : Product
Fields:
id int primary_key
class_id int
class_content CharField
Each class_id have multiple class content.
For two different class id there are same as well as different class_content.
I want to list the difference between two class_id:
Here is how I can create query in raw SQL:
eg: generate difference between class_id = 1 and class_id =2
Query:
SELECT * FROM Product
WHERE class_id = 1 && class_content NOT IN
(SELECT class_content FROM Product WHERE class_id = 2);
This query works fine and gives the difference between class_id 1 and 2. I want this query to be executed in django, I couldn't draw same result from django APIs.
Upvotes: 0
Views: 1831
Reputation: 11155
lista = Product.objects.values('class_content').filter(class_id=2)
Product.objects.filter(class_id = 1).exclude(class_content__in=lista)
Upvotes: 1
Reputation: 635
This should work:
Product.objects.filter(class_id=1).exclude(class_content__in=Product.objects.filter(class_id=2).values_list('class_content', flat=True))
Upvotes: 1