Django Query: how to use the sql function "union" and "not in"

How can I use the pool and the "not" in the request for Django. I searched for him, but I can not find any examples

SELECT id,address FROM tbl_nt WHERE st_id IN (1,2) AND name = 'foo' UNION ( SELECT d.id,d.addrses FROM tbl_nt_123 d WHERE d.name='foo' AND condition_id NOT IN ( SELECT condition_id FROM tbl_conditions WHERE version_id = 5 ) ) 

I tried this query to the bottom, but it did not work

 tbl_nt_123.objects.values_list('id', 'address').exclude( condition_id=tbl_conditions ).objects.filter(version_id=5).values_list( 'condition_id', flat=True) ) '). exclude ( tbl_nt_123.objects.values_list('id', 'address').exclude( condition_id=tbl_conditions ).objects.filter(version_id=5).values_list( 'condition_id', flat=True) ) 

How can i do this?

Please email me some good links or books to understand Django's preliminary requests.

thanks

+6
source share
1 answer

You should probably just add a search modifier __in :

 tbl_nt_123.objects.values_list('id','address').exclude( condition_id__in=tbl_conditions.objects.filter(version_id=5).values_list('condition_id',flat=True)) 

With regard to the association, you can fake it with the help of the operator | .

 union = queryset1 | queryset2 
+7
source

All Articles