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:

class Album(models.Model):
    name = models.CharField(max_length=255, db_index=True)
    artist = models.ForeignKey(Artist, on_delete=models.CASCADE, related_name='albums')
    year = models.IntegerField()

We want to create a form to create an album. 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):

form = Form.create(
    auto__model=Album,
    auto__exclude=['artist'],
)
▼ Hide result
form = Form.create(
    auto=dict(
        model=Album,
        exclude=['artist'],
    ),
)
► Show result
form = Form.create(
    auto__model=Album,
    fields__artist__include=False,
)
► Show result
class ArtistForm(Form):
    class Meta:
        auto__model = Album
        auto__exclude = ['artist']

form = ArtistForm.create()
► Show result
class ArtistForm(Form):
    class Meta:
        auto__model = Album
        auto__include = ['name', 'year']

form = ArtistForm.create()
► Show result
class ArtistForm(Form):
    class Meta:
        auto__model = Album
        fields__artist__include = False

form = ArtistForm.create()
► Show result

Without using the auto features:

class ArtistForm(Form):
    name = Field()
    year = Field.integer()

    class Meta:
        title = 'Create album'
        actions__submit__post_handler = create_artist

form = ArtistForm()
► Show result
form = Form(
    fields__name=Field(),
    fields__year=Field.integer(),
    title='Create album',
    actions__submit__post_handler=create_artist,
)
► Show result

You can read more about this in the philosophy section under Declarative/programmatic hybrid API.