@@ -53,8 +53,8 @@ You can use the ``<, >, <=, >=`` operators, which work as you expect.
53
53
54
54
-------------------------------------------------------------------------------
55
55
56
- like / ilike
57
- -------------
56
+ `` like `` / `` ilike ``
57
+ --------------------
58
58
59
59
The percentage operator is required to designate where the match should occur.
60
60
@@ -80,8 +80,8 @@ The percentage operator is required to designate where the match should occur.
80
80
81
81
-------------------------------------------------------------------------------
82
82
83
- not_like
84
- --------
83
+ `` not_like ``
84
+ ------------
85
85
86
86
Usage is the same as ``like `` excepts it excludes matching rows.
87
87
@@ -93,8 +93,8 @@ Usage is the same as ``like`` excepts it excludes matching rows.
93
93
94
94
-------------------------------------------------------------------------------
95
95
96
- is_in / not_in
97
- --------------
96
+ `` is_in `` / `` not_in ``
97
+ ----------------------
98
98
99
99
You can get all rows with a value contained in the list:
100
100
@@ -114,8 +114,8 @@ And all rows with a value not contained in the list:
114
114
115
115
-------------------------------------------------------------------------------
116
116
117
- is_null / is_not_null
118
- ---------------------
117
+ `` is_null `` / `` is_not_null ``
118
+ -----------------------------
119
119
120
120
These queries work, but some linters will complain about doing a comparison
121
121
with ``None ``:
@@ -202,8 +202,8 @@ Also, multiple arguments inside ``where`` clause is equivalent to an AND.
202
202
Band.popularity >= 100 , Band.popularity < 1000
203
203
)
204
204
205
- Using And / Or directly
206
- ~~~~~~~~~~~~~~~~~~~~~~~
205
+ Using `` And `` / `` Or `` directly
206
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
207
207
208
208
Rather than using the ``| `` and ``& `` characters, you can use the ``And `` and
209
209
``Or `` classes, which are what's used under the hood.
@@ -221,8 +221,8 @@ Rather than using the ``|`` and ``&`` characters, you can use the ``And`` and
221
221
222
222
-------------------------------------------------------------------------------
223
223
224
- WhereRaw
225
- --------
224
+ `` WhereRaw ``
225
+ ------------
226
226
227
227
In certain situations you may want to have raw SQL in your where clause.
228
228
@@ -268,3 +268,22 @@ The ``where`` clause has full support for joins. For example:
268
268
269
269
>> > await Band.select(Band.name).where(Band.manager.name == ' Guido' )
270
270
[{' name' : ' Pythonistas' }]
271
+
272
+ -------------------------------------------------------------------------------
273
+
274
+ Conditional ``where `` clauses
275
+ -----------------------------
276
+
277
+ You can add ``where `` clauses conditionally (e.g. based on user input):
278
+
279
+ .. code-block :: python
280
+
281
+ async def get_band_names (only_popular_bands : bool ) -> list[str ]:
282
+ query = Band.select(Band.name).output(as_list = True )
283
+
284
+ if only_popular_bands:
285
+ query = query.where(Band.popularity >= 1000 )
286
+
287
+ return await query
288
+
289
+ .. hint :: This works with all clauses, not just ``where`` clauses.
0 commit comments