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 = Form.create( auto__model=Album, auto__exclude=['artist'], )
form = Form.create( auto=dict( model=Album, exclude=['artist'], ), )
form = Form.create( auto__model=Album, fields__artist__include=False, )
class ArtistForm(Form): class Meta: auto__model = Album auto__exclude = ['artist'] form = ArtistForm.create()
class ArtistForm(Form): class Meta: auto__model = Album auto__include = ['name', 'year'] form = ArtistForm.create()
class ArtistForm(Form): class Meta: auto__model = Album fields__artist__include = False form = ArtistForm.create()
Without using the
class ArtistForm(Form): name = Field() year = Field.integer() class Meta: title = 'Create album' actions__submit__post_handler = create_artist form = ArtistForm()
form = Form( fields__name=Field(), fields__year=Field.integer(), title='Create album', actions__submit__post_handler=create_artist, )
You can read more about this in the philosophy section under Declarative/programmatic hybrid API.