django mysql filter_django 数据库中的查询 filter()和exclude()笔记

XX.objects.filter(条件A,条件B) -------------》一个对象 两个条件都满足

例:

Person.objects.filter(supermarket__name__contains='乐', supermarket__area__gte=500).query.__str__()

查看其sql语句为:

'SELECT "test_chaxun_app_person"."id", "test_chaxun_app_person"."name", "test_chaxun_app_person"."old", "test_chaxun_app_person"."sex" FROM "test_chaxun_app_person" INNER JOIN "test_chaxun_app_supermarket_person" ON ("test_chaxun_app_person"."id" = "test_chaxun_app_supermarket_person"."person_id") INNER JOIN "test_chaxun_app_supermarket" ON ("test_chaxun_app_supermarket_person"."supermarket_id" = "test_chaxun_app_supermarket"."id") WHERE ("test_chaxun_app_supermarket"."area" >= 500 AND "test_chaxun_app_supermarket"."name"::text LIKE %乐%)'

XX.objects.filter(条件A).filter(条件B) -------------》有一个条件满足条件A还有一个对象满足条件B

例:

Person.objects.filter(supermarket__name__contains='乐').filter(supermarket__area__gte=500).query.__str__()

查看其sql 语句为:

'SELECT "test_chaxun_app_person"."id", "test_chaxun_app_person"."name", "test_chaxun_app_person"."old", "test_chaxun_app_person"."sex" FROM "test_chaxun_app_person" INNER JOIN "test_chaxun_app_supermarket_person" ON ("test_chaxun_app_person"."id" = "test_chaxun_app_supermarket_person"."person_id") INNER JOIN "test_chaxun_app_supermarket" ON ("test_chaxun_app_supermarket_person"."supermarket_id" = "test_chaxun_app_supermarket"."id") INNER JOIN "test_chaxun_app_supermarket_person" T4 ON ("test_chaxun_app_person"."id" = T4."person_id") INNER JOIN "test_chaxun_app_supermarket" T5 ON (T4."supermarket_id" = T5."id") WHERE ("test_chaxun_app_supermarket"."name"::text LIKE %乐% AND T5."area" >= 500)'

____________________________________自己添加分割线————————————————

XX.objects.exclude(条件A,条件B)------------------------------>排除

Person.objects.exclude(supermarket__name__contains='乐', supermarket__area__gte=500).query.__str__()

SELECT "test_chaxun_app_person"."id", "test_chaxun_app_person"."name", "test_chaxun_app_person"."old", "test_chaxun_app_person"."sex" FROM "test_chaxun_app_person" WHERE NOT ("test_chaxun_app_person"."id" IN (SELECT U1."person_id" FROM "test_chaxun_app_supermarket_person" U1 INNER JOIN "test_chaxun_app_supermarket" U2 ON (U1."supermarket_id" = U2."id") WHERE U2."area" >= 500) AND "test_chaxun_app_person"."id" IN (SELECT U1."person_id" FROM "test_chaxun_app_supermarket_person" U1 INNER JOIN "test_chaxun_app_supermarket" U2 ON (U1."supermarket_id" = U2."id") WHERE U2."name"::text LIKE %乐%))

XX.objects.exclude(条件A).exclude(条件B)--------------------->排除

例:

Person.objects.exclude(supermarket__name__contains='乐').exclude(supermarket__area__gte=500)

SELECT "test_chaxun_app_person"."id", "test_chaxun_app_person"."name", "test_chaxun_app_person"."old", "test_chaxun_app_person"."sex" FROM "test_chaxun_app_person" WHERE (NOT ("test_chaxun_app_person"."id" IN (SELECT U1."person_id" FROM "test_chaxun_app_supermarket_person" U1 INNER JOIN "test_chaxun_app_supermarket" U2 ON (U1."supermarket_id" = U2."id") WHERE U2."name"::text LIKE %乐%)) AND NOT ("test_chaxun_app_person"."id" IN (SELECT U1."person_id" FROM "test_chaxun_app_supermarket_person" U1 INNER JOIN "test_chaxun_app_supermarket" U2 ON (U1."supermarket_id" = U2."id") WHERE U2."area" >= 500)))

Person.objects.exclude(supermarket__name__contains='乐').exclude(supermarket__area__gte=500).count() -------》264

Person.objects.filter(~models.Q(supermarket__name__contains='乐') ).filter( ~models.Q(supermarket__area__gte=500)).count() --------》264

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值