Skip to content

Commit 3c16e08

Browse files
committed
improved docs - added comparable Django queries
- Added a new page showing the equivalent of common Django queries in Piccolo. - Renamed `Create` query page to `Create Table`. - General tidy up.
1 parent 76c2cb5 commit 3c16e08

File tree

5 files changed

+176
-26
lines changed

5 files changed

+176
-26
lines changed

docs/src/piccolo/getting_started/example_schema.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This is the schema used by the example queries throughout the docs.
88
.. code-block:: python
99
1010
from piccolo.table import Table
11-
from piccolo.columns import ForeignKey, Varchar
11+
from piccolo.columns import ForeignKey, Integer, Varchar
1212
1313
1414
class Manager(Table):
@@ -18,5 +18,6 @@ This is the schema used by the example queries throughout the docs.
1818
class Band(Table):
1919
name = Varchar(length=100)
2020
manager = Varchar(length=100)
21+
popularity = Integer()
2122
2223
To understand more about defining your own schemas, see :ref:`DefiningSchema`.

docs/src/piccolo/query_types/create.rst renamed to docs/src/piccolo/query_types/create_table.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.. _Create:
22

3-
Create
4-
======
3+
Create Table
4+
============
55

66
This creates the table and columns in the database.
77

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
.. _DjangoComparison:
2+
3+
Django Comparison
4+
=================
5+
6+
Here are some common queries, showing how they're done in Django vs Piccolo.
7+
All of the Piccolo examples can also be run :ref:`asynchronously<SyncAndAsync>`.
8+
9+
Queries
10+
-------
11+
12+
create
13+
~~~~~~
14+
15+
.. code-block:: python
16+
17+
# Django
18+
>>> band = Band(name="Pythonistas")
19+
>>> band.save()
20+
>>> band
21+
<Band: 1>
22+
23+
# Piccolo
24+
>>> band = Band(name="Pythonistas")
25+
>>> band.save().run_sync()
26+
>>> band
27+
<Band: 1>
28+
29+
update
30+
~~~~~~
31+
32+
.. code-block:: python
33+
34+
# Django
35+
>>> band = Band.objects.get(name="Pythonistas")
36+
>>> band
37+
<Band: 1>
38+
>>> band.name = "Amazing Band"
39+
>>> band.save()
40+
41+
# Piccolo
42+
>>> band = Band.objects().where(Band.name == 'Pythonistas').first().run_sync()
43+
>>> band
44+
<Band: 1>
45+
>>> band.name = "Amazing Band"
46+
>>> band.save().run_sync()
47+
48+
delete
49+
~~~~~~
50+
51+
Individual rows:
52+
53+
.. code-block:: python
54+
55+
# Django
56+
>>> band = Band.objects.get(name="Pythonistas")
57+
>>> band.delete()
58+
59+
# Piccolo
60+
>>> band = Band.objects().where(Band.name == 'Pythonistas').first().run_sync()
61+
>>> band.remove().run_sync()
62+
63+
In bulk:
64+
65+
.. code-block:: python
66+
67+
# Django
68+
>>> Band.objects.filter(popularity__lt=1000).delete()
69+
70+
# Piccolo
71+
>>> Band.delete().where(Band.popularity < 1000).delete().run_sync()
72+
73+
filter
74+
~~~~~~
75+
76+
.. code-block:: python
77+
78+
# Django
79+
>>> Band.objects.filter(name="Pythonistas")
80+
[<Band: 1>]
81+
82+
# Piccolo
83+
>>> Band.objects().where(Band.name == "Pythonistas").run_sync()
84+
[<Band: 1>]
85+
86+
values_list
87+
~~~~~~~~~~~
88+
89+
.. code-block:: python
90+
91+
# Django
92+
>>> Band.objects.values_list('name')
93+
[{'name': 'Pythonistas'}, {'name': 'Rustaceans'}]
94+
95+
# Piccolo
96+
>>> Band.select(Band.name).run_sync()
97+
[{'name': 'Pythonistas'}, {'name': 'Rustaceans'}]
98+
99+
With ``flat=True``:
100+
101+
.. code-block:: python
102+
103+
# Django
104+
>>> Band.objects.values_list('name', flat=True)
105+
['Pythonistas', 'Rustaceans']
106+
107+
# Piccolo
108+
>>> Band.select(Band.name).output(as_list=True).run_sync()
109+
['Pythonistas', 'Rustaceans']
110+
111+
-------------------------------------------------------------------------------
112+
113+
Database Settings
114+
-----------------
115+
116+
In Django you configure your database in ``settings.py``. With Piccolo, you
117+
definte an ``Engine`` in ``piccolo_conf.py``. See :ref:`Engines`.
Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
11
Query Types
22
===========
33

4-
There are many different queries you can perform using Piccolo. For starters,
5-
focus on `select`, `insert` and `update`.
4+
There are many different queries you can perform using Piccolo.
5+
6+
The main ways to query data are with :ref:`Select`, which returns data as
7+
dictionaries, and :ref:`Objects` , which returns data as class instances, like a
8+
typical ORM.
69

710
.. toctree::
8-
:maxdepth: 0
11+
:maxdepth: 1
912

1013
./select
14+
./objects
1115
./alter
1216
./count
13-
./create
17+
./create_table
1418
./delete
1519
./exists
1620
./insert
17-
./objects
1821
./raw
1922
./update
2023
./transactions
24+
25+
Comparisons
26+
-----------
27+
28+
If you're familiar with other ORMs, here are some guides which show the Piccolo
29+
equivalents of common queries.
30+
31+
.. toctree::
32+
:maxdepth: 1
33+
34+
./django_comparison

docs/src/piccolo/query_types/objects.rst

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,55 @@ Objects
44
=======
55

66
When doing :ref:`Select` queries, you get data back in the form of a list of
7-
dictionaries (where each dictionary represents a row).
7+
dictionaries (where each dictionary represents a row). This is useful in a lot
8+
of situations, but it's sometimes preferable to get objects back instead, as we
9+
can manipulate them, and save the changes back to the database.
810

9-
This is useful in a lot of situations, but it's also useful to get objects
10-
back instead.
11+
In Piccolo, an instance of a ``Table`` class represents a row. Let's do some
12+
examples.
1113

12-
In Piccolo, an instance of a Table represents a row. Let's do an
13-
example.
14+
Fetching objects
15+
----------------
16+
17+
To get all objects:
1418

1519
.. code-block:: python
1620
17-
# To get all objects:
18-
Band.objects().run_sync()
21+
>>> Band.objects().run_sync()
22+
[<Band: 1>, <Band: 2>]
23+
24+
To get certain rows:
25+
26+
.. code-block:: python
27+
28+
>>> Band.objects().where(Band.name == 'Pythonistas').run_sync()
29+
[<Band: 1>]
30+
31+
To get the first row:
1932

20-
# To get certain rows:
21-
Band.objects().where(Band.name == 'Pythonistas').run_sync()
33+
.. code-block:: python
2234
23-
# Get the first row
24-
Band.objects().first().run_sync()
35+
>>> Band.objects().first().run_sync()
36+
<Band: 1>
2537
26-
You'll notice that the API is similar to `select` - except it returns all
38+
You'll notice that the API is similar to :ref:`Select` - except it returns all
2739
columns.
2840

29-
Saving objects
30-
--------------
41+
Creating objects
42+
----------------
43+
44+
.. code-block:: python
45+
46+
>>> band = Band(name="C-Sharps", popularity=100)
47+
>>> band.save().run_sync()
48+
49+
Updating objects
50+
----------------
3151

32-
Objects have a `save` method, which is convenient for updating values:
52+
Objects have a ``save`` method, which is convenient for updating values:
3353

3454
.. code-block:: python
3555
36-
# To get certain rows:
3756
pythonistas = Band.objects().where(
3857
Band.name == 'Pythonistas'
3958
).first().run_sync()
@@ -44,11 +63,10 @@ Objects have a `save` method, which is convenient for updating values:
4463
Deleting objects
4564
----------------
4665

47-
Similarly, we can delete objects, using the `remove` method.
66+
Similarly, we can delete objects, using the ``remove`` method.
4867

4968
.. code-block:: python
5069
51-
# To get certain rows:
5270
pythonistas = Band.objects().where(
5371
Band.name == 'Pythonistas'
5472
).first().run_sync()

0 commit comments

Comments
 (0)