Skip to content

Commit 47e5305

Browse files
authored
add docs for conditional where clauses (#1074)
1 parent 716b7f0 commit 47e5305

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

Diff for: docs/src/piccolo/query_clauses/where.rst

+31-12
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ You can use the ``<, >, <=, >=`` operators, which work as you expect.
5353
5454
-------------------------------------------------------------------------------
5555

56-
like / ilike
57-
-------------
56+
``like`` / ``ilike``
57+
--------------------
5858

5959
The percentage operator is required to designate where the match should occur.
6060

@@ -80,8 +80,8 @@ The percentage operator is required to designate where the match should occur.
8080

8181
-------------------------------------------------------------------------------
8282

83-
not_like
84-
--------
83+
``not_like``
84+
------------
8585

8686
Usage is the same as ``like`` excepts it excludes matching rows.
8787

@@ -93,8 +93,8 @@ Usage is the same as ``like`` excepts it excludes matching rows.
9393
9494
-------------------------------------------------------------------------------
9595

96-
is_in / not_in
97-
--------------
96+
``is_in`` / ``not_in``
97+
----------------------
9898

9999
You can get all rows with a value contained in the list:
100100

@@ -114,8 +114,8 @@ And all rows with a value not contained in the list:
114114
115115
-------------------------------------------------------------------------------
116116

117-
is_null / is_not_null
118-
---------------------
117+
``is_null`` / ``is_not_null``
118+
-----------------------------
119119

120120
These queries work, but some linters will complain about doing a comparison
121121
with ``None``:
@@ -202,8 +202,8 @@ Also, multiple arguments inside ``where`` clause is equivalent to an AND.
202202
Band.popularity >= 100, Band.popularity < 1000
203203
)
204204
205-
Using And / Or directly
206-
~~~~~~~~~~~~~~~~~~~~~~~
205+
Using ``And`` / ``Or`` directly
206+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
207207

208208
Rather than using the ``|`` and ``&`` characters, you can use the ``And`` and
209209
``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
221221
222222
-------------------------------------------------------------------------------
223223

224-
WhereRaw
225-
--------
224+
``WhereRaw``
225+
------------
226226

227227
In certain situations you may want to have raw SQL in your where clause.
228228

@@ -268,3 +268,22 @@ The ``where`` clause has full support for joins. For example:
268268
269269
>>> await Band.select(Band.name).where(Band.manager.name == 'Guido')
270270
[{'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

Comments
 (0)