Skip to content

Commit 2c9d798

Browse files
committed
added docs for load_json and added an example to the playground
1 parent 6d99086 commit 2c9d798

File tree

5 files changed

+79
-16
lines changed

5 files changed

+79
-16
lines changed

docs/src/piccolo/query_clauses/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ by modifying the return values.
1414
./limit
1515
./offset
1616
./order_by
17+
./output
1718
./where
1819
./batch
1920
./freeze
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
.. _output:
2+
3+
output
4+
======
5+
6+
You can use ``output`` clauses with the following queries:
7+
8+
* :ref:`Select`
9+
* :ref:`Objects`
10+
11+
-------------------------------------------------------------------------------
12+
13+
Select queries only
14+
-------------------
15+
16+
as_json
17+
~~~~~~~
18+
19+
To return the data as a JSON string:
20+
21+
.. code-block:: python
22+
23+
>>> Band.select().output(as_json=True).run_sync()
24+
'[{"name":"Pythonistas","manager":1,"popularity":1000,"id":1},{"name":"Rustaceans","manager":2,"popularity":500,"id":2}]'
25+
26+
Piccolo can use `orjson <https://github.com/ijl/orjson>`_ for JSON serialisation,
27+
which is blazing fast, and can handle most Python types, including dates,
28+
datetimes, and UUIDs. To install Piccolo with orjson support use
29+
``pip install piccolo[orjson]``.
30+
31+
as_list
32+
~~~~~~~
33+
34+
If you're just querying a single column from a database table, you can use
35+
``as_list`` to flatten the results into a single list.
36+
37+
.. code-block:: python
38+
39+
>>> Band.select(Band.id).output(as_list=True).run_sync()
40+
[1, 2]
41+
42+
-------------------------------------------------------------------------------
43+
44+
Select and Objects queries
45+
--------------------------
46+
47+
load_json
48+
~~~~~~~~~
49+
50+
If querying JSON or JSONB columns, you can tell Piccolo to deserialise the JSON
51+
values automatically.
52+
53+
.. code-block:: python
54+
55+
>>> RecordingStudio.select().output(load_json=True).run_sync()
56+
[{'id': 1, 'name': 'Abbey Road', 'facilities': {'restaurant': True, 'mixing_desk': True}}]
57+
58+
>>> studio = RecordingStudio.objects().first().output(load_json=True).run_sync()
59+
>>> studio.facilities
60+
{'restaurant': True, 'mixing_desk': True}

docs/src/piccolo/query_types/objects.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ order_by
117117

118118
See  :ref:`order_by`.
119119

120+
output
121+
~~~~~~
122+
123+
See  :ref:`output`.
124+
120125
where
121126
~~~~~
122127

docs/src/piccolo/query_types/select.rst

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -147,21 +147,7 @@ See  :ref:`order_by`.
147147
output
148148
~~~~~~
149149

150-
By default, the data is returned as a list of dictionaries (where each
151-
dictionary represents a row). This can be altered using the ``output`` method.
152-
153-
To return the data as a JSON string:
154-
155-
.. code-block:: python
156-
157-
>>> b = Band
158-
>>> b.select().output(as_json=True).run_sync()
159-
'[{"name":"Pythonistas","manager":1,"popularity":1000,"id":1},{"name":"Rustaceans","manager":2,"popularity":500,"id":2}]'
160-
161-
Piccolo can use `orjson <https://github.com/ijl/orjson>`_ for JSON serialisation,
162-
which is blazing fast, and can handle most Python types, including dates,
163-
datetimes, and UUIDs. To install Piccolo with orjson support use
164-
``pip install piccolo[orjson]``.
150+
See :ref:`output`.
165151

166152
where
167153
~~~~~

piccolo/apps/playground/commands/run.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
ForeignKey,
1515
Integer,
1616
Interval,
17+
JSON,
1718
Numeric,
1819
Timestamp,
1920
UUID,
@@ -55,7 +56,12 @@ class DiscountCode(Table):
5556
active = Boolean(default=True, null=True)
5657

5758

58-
TABLES = (Manager, Band, Venue, Concert, Ticket, DiscountCode)
59+
class RecordingStudio(Table):
60+
name = Varchar(length=100)
61+
facilities = JSON()
62+
63+
64+
TABLES = (Manager, Band, Venue, Concert, Ticket, DiscountCode, RecordingStudio)
5965

6066

6167
def populate():
@@ -105,6 +111,11 @@ def populate():
105111
discount_code = DiscountCode(code=uuid.uuid4())
106112
discount_code.save().run_sync()
107113

114+
recording_studio = RecordingStudio(
115+
name="Abbey Road", facilities={"restaurant": True, "mixing_desk": True}
116+
)
117+
recording_studio.save().run_sync()
118+
108119

109120
def run(
110121
engine: str = "sqlite",

0 commit comments

Comments
 (0)