问题描述:

How can use union and "not in" function in django query. I have searched on it but cannot find any example

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 have tried this query for lower portion but didn't 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))

How can i do this?

Please refer me some good links or books for understand advance django queries.

Thank you

网友答案:

Probably you should just add the __in lookup modifier:

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))

As for the union, you can fake it using the | operator.

union = queryset1 | queryset2
相关阅读:
Top