Equivalence
===========
In iommi there are multiple ways to accomplish the same thing. The two most obvious ways are declarative and programmatic. But there are different paths even within those two main paths. This page is an overview of a few of those ways. Hopefully you will see the philosophy through these examples. Let's get started!
First a model:
.. literalinclude:: models.py
:start-at: class Album
:end-before: def __str__
:language: python
We want to create a form to create an album for a specific artist. We already have the artist from the URL, so that field shouldn't be in the form.
The following forms all accomplish this goal (you can use `form.as_view()` to create a view from a `Form` instance):
.. code-block:: python
form = Form.create(
auto__model=Album,
auto__exclude=['artist'],
)
.. raw:: html
▼ Hide result
.. code-block:: python
form = Form.create(
auto=dict(
model=Album,
exclude=['artist'],
),
)
.. raw:: html
► Show result
.. code-block:: python
form = Form.create(
auto__model=Album,
fields__artist__include=False,
)
.. raw:: html
► Show result
.. code-block:: python
class AlbumForm(Form):
class Meta:
auto__model = Album
auto__exclude = ['artist']
form = AlbumForm.create()
.. raw:: html
► Show result
.. code-block:: python
class AlbumForm(Form):
class Meta:
auto__model = Album
auto__include = ['name', 'year']
form = AlbumForm.create()
.. raw:: html
► Show result
.. code-block:: python
class AlbumForm(Form):
class Meta:
auto__model = Album
fields__artist__include = False
form = AlbumForm.create()
.. raw:: html
► Show result
Without using the `auto` features:
.. code-block:: python
class AlbumForm(Form):
name = Field()
year = Field.integer()
class Meta:
title = 'Create album'
actions__submit__post_handler = create_album
form = AlbumForm()
.. raw:: html
► Show result
.. code-block:: python
form = Form(
fields__name=Field(),
fields__year=Field.integer(),
title='Create album',
actions__submit__post_handler=create_album,
)
.. raw:: html
► Show result
You can read more about this in the philosophy section under :ref:`philosophy_hybrid_api`.