Skip to content

Commit b0983d7

Browse files
committed
Rewrite tutorial to do auto first
1 parent e8d523c commit b0983d7

File tree

7 files changed

+200
-147
lines changed

7 files changed

+200
-147
lines changed

.idea/dictionaries/boxed.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import pytest
2+
from django.urls import (
3+
path,
4+
)
5+
6+
from docs.models import (
7+
Album,
8+
)
9+
from iommi import (
10+
Column,
11+
Table,
12+
)
13+
from tests.helpers import (
14+
req,
15+
show_output,
16+
show_output_collapsed,
17+
)
18+
19+
pytestmark = pytest.mark.django_db
20+
21+
# Table(auto...)
22+
# Table(columns__foo=Column.from_model...)
23+
# Table(columns__foo=Column(....))
24+
25+
26+
# .as_view
27+
# FBV using middleware
28+
29+
30+
# reverse of
31+
32+
def test_foo():
33+
# language=rst
34+
"""
35+
36+
37+
38+
39+
We’ll start with using iommi declarative tables to create a list of albums:
40+
"""
41+
42+
class AlbumTable(Table):
43+
name = Column()
44+
artist = Column()
45+
year = Column()
46+
47+
def index(request):
48+
return AlbumTable(
49+
title='Albums',
50+
rows=Album.objects.all(),
51+
)
52+
53+
# @test
54+
show_output(index(req('get')))
55+
# @end
56+
57+
# language=rst
58+
"""
59+
The iommi middleware will handle if you return an iommi type and render it properly.
60+
61+
At this point you might think "Hold on! Where is the template?". There isn't one. We don't need a template. iommi works at a higher level of abstraction. Don't worry, you can drop down to templates if you need to though. This will be covered later.
62+
63+
64+
You get sorting and pagination by default, and we're using the default bootstrap 5 style. iommi ships with `more styles <style>`_ that you can switch to, or you can `implement your own custom style <style>`_.
65+
"""
66+
67+
68+
def test_class_meta():
69+
# language=rst
70+
"""
71+
class Meta
72+
==========
73+
74+
The `class Meta` concept in iommi is slightly different from how it's used in Django. In iommi any argument to the constructor of a class can be put into `Meta`. In fact, ONLY valid arguments to the constructor can be set in `Meta`. In our example above we set `title` and `rows`. We can also instead set them via `Meta`:
75+
"""
76+
77+
class AlbumTable(Table):
78+
name = Column()
79+
artist = Column()
80+
year = Column()
81+
82+
class Meta:
83+
title = 'Albums'
84+
rows = Album.objects.all()
85+
86+
def index(request):
87+
return AlbumTable()
88+
89+
# @test
90+
index(req('get'))
91+
# @end
92+
93+
# language=rst
94+
"""
95+
This will do the same thing! But with a slight twist: parameters set in `Meta` are just defaults, meaning you can still override them later in the constructor call (or in subclasses).
96+
97+
Using as_view
98+
=============
99+
100+
The view we have so far doesn't use the `request` argument. We can simplify it by doing this instead:
101+
"""
102+
103+
urlpatterns = [
104+
path('', AlbumTable().as_view()),
105+
]
106+
107+
# @test
108+
show_output_collapsed(urlpatterns[0])
109+
# @end
110+
111+
# language=rst
112+
"""
113+
This looks superficially similar to class based views, but they are very different! Notice the parenthesis after the class name for example. And Django CBVs can't be combined with iommi classes because they are radically different concepts.
114+
115+
That an instance of `AlbumTable` is created here means we can pass arguments here:
116+
"""
117+
118+
urlpatterns = [
119+
path('', AlbumTable(title='Other title', page_size=2).as_view()),
120+
]
121+
122+
# @test
123+
show_output_collapsed(urlpatterns[0])
124+
# @end
125+
126+
# language=rst
127+
"""
128+
129+
auto__model
130+
===========
131+
132+
The next step in the simplification is to realize that this table is trivially derived from the model definition. iommi has features to do this for you so we can simplify even further! We delete the entire `AlbumTable` class and replace the url definition with this single line:
133+
"""
134+
135+
urlpatterns = [
136+
path('', Table(auto__model=Album).as_view()),
137+
]
138+
139+
# @test
140+
show_output_collapsed(urlpatterns[0])
141+
# @end
142+
143+
# language=rst
144+
"""
145+
You don't even need to specify the title of the table, as we use the plural verbose name of the model. These are all defaults, not hard coded values, so you can pass parameters to the `Table` constructor here to override anything you want.
146+
"""

docs/test_doc_cookbook_forms.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
def test_forms():
2727
# language=rst
2828
"""
29+
.. _cookbook-forms:
30+
2931
Forms
3032
-----
3133

docs/test_doc_cookbook_tables.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
def test_tables():
2222
# language=rst
2323
"""
24+
.. _cookbook-tables:
25+
2426
Tables
2527
------
2628

docs/test_doc_styles.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
def test_style():
1313
# language=rst
1414
"""
15+
.. _style:
16+
1517
Style
1618
=====
1719

0 commit comments

Comments
 (0)