簡體   English   中英

Django ORM,以防萬一

[英]Django ORM for not in case

我正在嘗試將以下給定的SQL查詢轉換為Django ORM。

select usp.user_id from Class_A as usp join Class_B as au 
       on au.id = usp.user_id join Class_C as up 
       on up.user_id = au.id where up.type not in ('A','B','C') 
       and up.end_time > now() and up.user_id = usp.user_id 
       and usp.data_enabled=1 and (usp.item_state=3 or usp.item_state=4)  
       and usp.user_id=au.id and au.is_active=1

我需要過濾出那些類型不是('A','B','C')之一的行

我嘗試了多種組合,但結果卻出錯了,因為無法以正確的方式up.plan_type not in ('A','B','C')中未實現up.plan_type not in ('A','B','C') 我嘗試使用exclude()~Q()變量,但是它們沒有給出正確的結果,因為它們在執行所有聯接操作后會排除行,我在進行聯接操作時需要排除行。

我試過了

ClassA.objects.exclude(user__classA_plans__plan_type__in = ['I', 'C', 'D']).filter(user__classA_plans__end_time__gte = datetime.now(),**query)

其中classA_plans是相關名稱。

請幫我。

Django ORM當前不支持條件連接,您將不得不重寫查詢,使用原始sql或切換到其他SQL或類似SQLAlchemy的ORM引擎。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM