简体   繁体   English

Django ORM,以防万一

[英]Django ORM for not in case

I am trying to convert below given SQL query to Django ORM. 我正在尝试将以下给定的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

I need to filter out those rows whose type is not one of ('A', 'B', 'C') 我需要过滤出那些类型不是('A','B','C')之一的行

I tried many combination but I am getting wrong result because I could not implement up.plan_type not in ('A','B','C') in right way. 我尝试了多种组合,但结果却出错了,因为无法以正确的方式up.plan_type not in ('A','B','C')中未实现up.plan_type not in ('A','B','C') I tried using exclude() and ~Q() variable but they are not giving right result because they are excluding rows after doing all join operation, I need to exclude rows while doing join operation. 我尝试使用exclude()~Q()变量,但是它们没有给出正确的结果,因为它们在执行所有联接操作后会排除行,我在进行联接操作时需要排除行。

I tried this 我试过了

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

where classA_plans is related name. 其中classA_plans是相关名称。

Please help me on this. 请帮我。

Django ORM当前不支持条件连接,您将不得不重写查询,使用原始sql或切换到其他SQL或类似SQLAlchemy的ORM引擎。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM