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