1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126 | # ======================================================================
# The remaining lookup variants: `__contained_by` (arrays/json/ranges — the
# inverse of `__contains`), `__fully_gt`, range bound transforms like
# `__lower_inc`, JSON `__has_keys`/`__has_any_keys`, and
# `__trigram_word_similar` (a fuzzy word match inside a longer string).
# ======================================================================
# ----------------------------------------------------------------------
# tags contained_by a superset
# ----------------------------------------------------------------------
# Django:
Book.objects.filter(tags__contained_by=["fantasy","magic","epic","adventure","tech","database"])
# SQL:
# SELECT "examples_book"."id", "examples_book"."title", "examples_book"."author_id", "examples_book"."price", "examples_book"."pages", "examples_book"."published", "examples_book"."is_active", "examples_book"."tags", "examples_book"."data", "examples_book"."search", "examples_book"."title_upper"
# FROM "examples_book"
# WHERE "examples_book"."tags" <@ (ARRAY['fantasy', 'magic', 'epic', 'adventure', 'tech', 'database'])::varchar(30)[]
# Result:
# Harry Potter
# The Hobbit
# The Lord of the Rings
# Postgres for Authors
# ----------------------------------------------------------------------
# data has_keys ['isbn', 'specs']
# ----------------------------------------------------------------------
# Django:
Book.objects.filter(data__has_keys=["isbn", "specs"])
# SQL:
# SELECT "examples_book"."id", "examples_book"."title", "examples_book"."author_id", "examples_book"."price", "examples_book"."pages", "examples_book"."published", "examples_book"."is_active", "examples_book"."tags", "examples_book"."data", "examples_book"."search", "examples_book"."title_upper"
# FROM "examples_book"
# WHERE "examples_book"."data" ?& '{isbn,specs}'
# Result:
# Harry Potter
# The Hobbit
# The Lord of the Rings
# Postgres for Authors
# ----------------------------------------------------------------------
# data has_any_keys ['isbn', 'missing']
# ----------------------------------------------------------------------
# Django:
Book.objects.filter(data__has_any_keys=["isbn", "missing"])
# SQL:
# SELECT "examples_book"."id", "examples_book"."title", "examples_book"."author_id", "examples_book"."price", "examples_book"."pages", "examples_book"."published", "examples_book"."is_active", "examples_book"."tags", "examples_book"."data", "examples_book"."search", "examples_book"."title_upper"
# FROM "examples_book"
# WHERE "examples_book"."data" ?| '{isbn,missing}'
# Result:
# Harry Potter
# The Hobbit
# The Lord of the Rings
# Postgres for Authors
# ----------------------------------------------------------------------
# price_band fully above [0, 50) (__fully_gt)
# ----------------------------------------------------------------------
# Django:
Reservation.objects.filter(price_band__fully_gt=Range(0, 50))
# SQL:
# SELECT "examples_reservation"."id", "examples_reservation"."room", "examples_reservation"."during", "examples_reservation"."price_band", "examples_reservation"."date_span", "examples_reservation"."amount", "examples_reservation"."seq"
# FROM "examples_reservation"
# WHERE "examples_reservation"."price_band" >> '[0,50)'
# Result:
# room 101 [2026-06-01 00:00:00+00:00, 2026-06-05 00:00:00+00:00)
# room 101 [2026-06-05 00:00:00+00:00, 2026-06-09 00:00:00+00:00)
# room 202 [2026-06-02 00:00:00+00:00, 2026-06-06 00:00:00+00:00)
# ----------------------------------------------------------------------
# during contained_by [6/1, 6/30) (__contained_by)
# ----------------------------------------------------------------------
# Django:
Reservation.objects.filter(during__contained_by=Range(dt(2026,6,1), dt(2026,6,30)))
# SQL:
# SELECT "examples_reservation"."id", "examples_reservation"."room", "examples_reservation"."during", "examples_reservation"."price_band", "examples_reservation"."date_span", "examples_reservation"."amount", "examples_reservation"."seq"
# FROM "examples_reservation"
# WHERE "examples_reservation"."during" <@ '["2026-06-01 00:00:00+00:00","2026-06-30 00:00:00+00:00")'::tstzrange
# Result:
# room 101 [2026-06-01 00:00:00+00:00, 2026-06-05 00:00:00+00:00)
# room 101 [2026-06-05 00:00:00+00:00, 2026-06-09 00:00:00+00:00)
# room 202 [2026-06-02 00:00:00+00:00, 2026-06-06 00:00:00+00:00)
# ----------------------------------------------------------------------
# price_band lower bound inclusive (__lower_inc=True)
# ----------------------------------------------------------------------
# Django:
Reservation.objects.filter(price_band__lower_inc=True)
# SQL:
# SELECT "examples_reservation"."id", "examples_reservation"."room", "examples_reservation"."during", "examples_reservation"."price_band", "examples_reservation"."date_span", "examples_reservation"."amount", "examples_reservation"."seq"
# FROM "examples_reservation"
# WHERE LOWER_INC("examples_reservation"."price_band")
# Result:
# room 101 [2026-06-01 00:00:00+00:00, 2026-06-05 00:00:00+00:00)
# room 101 [2026-06-05 00:00:00+00:00, 2026-06-09 00:00:00+00:00)
# room 202 [2026-06-02 00:00:00+00:00, 2026-06-06 00:00:00+00:00)
# ----------------------------------------------------------------------
# title trigram-word-similar to 'lord' (__trigram_word_similar)
# ----------------------------------------------------------------------
# Django:
Book.objects.filter(title__trigram_word_similar="lord")
# SQL:
# SELECT "examples_book"."id", "examples_book"."title", "examples_book"."author_id", "examples_book"."price", "examples_book"."pages", "examples_book"."published", "examples_book"."is_active", "examples_book"."tags", "examples_book"."data", "examples_book"."search", "examples_book"."title_upper"
# FROM "examples_book"
# WHERE "examples_book"."title" %> 'lord'
# Result:
# The Lord of the Rings
|