打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Django sql in查询

I'm trying to figure out how to write a 'not in' style query in django.For example, the query structure I'm thinking of would look like this.

select table1.* from table1where table1.id not in (  select table2.key_to_table1  from table2   where table2.id = some_parm )

What would the django syntax look like assuming models called table1 and table2?

asked Dec 23 '10 at 23:42
Turbo
73921123
add comment

4 Answers

up vote 37 down vote accepted

table1.objects.exclude(id__in = table2.objects.filter(your_condition).values_list('id', flat=True))

The exclude function works like the Not operator you where asking for. The attribute flat = True tells to table2 query to return the value_list as a one level list. So... at the end you are obtaining a list of IDs from table2, which you are going to user to define the condition in table1, that will be denied by the exclude function.

answered Aug 26 '11 at 4:49
Harph
505512
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Hql语句注意事项总结
运行python manage.py test会出现“达到最大递归深度”错误
一篇文章带你了解Django ORM操作(进阶篇)
Hibernate中用HQL执行查询
python3读取excel文件只提取某些行某些列的值
Power Query中的Transform怎么用?
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服