Edit tables¶
How do you edit one-to-one fields in an edit table?¶
Include them in auto__include. Say you have a profile model for an artist:
Then you can include the artist name field:
edit_table = EditTable(
auto__model=Profile,
auto__include=['artist__name'],
columns__artist_name__field__include=True,
)
How do I include labels for fields?¶
If you’re using EditTable and not EditTable.div, then by default you get fields rendered without labels,
because the label text is in the table header. But in case you still want to render labels (e.g. as floating labels),
you can just set extra_evaluated__input_labels_include = True:
edit_table = EditTable(
auto__model=Profile,
auto__include=['artist__name'],
columns__artist_name__field__include=True,
extra_evaluated__input_labels_include=True,
)
How do I let users drag to reorder rows?¶
The easiest way to make a manually reorderable table is to inherit from iommi.models.Orderable.
This gives your model a sort_order field, a default order_by set on the model to sort on that field,
and that field has the iommi.model_fields.SortOrderField which is mapped to the correct column type automatically.
If you do this then set reorderable=True on an EditTable to get drag-and-drop reordering
of the rows. You can also pass a dict of SortableJS options instead of True:
edit_table = EditTable(
auto__model=FavoriteArtist,
auto__include=['artist__name', 'comment', 'sort_order'],
columns__comment__field__include=True,
reorderable=True,
sortable=False,
)
How do I configure the edit and create forms of an edit table?¶
An EditTable builds two forms behind the scenes: edit_form for the existing
rows and create_form for new rows added via “Add row”. Both are configured
through the matching namespaces:
edit_table = EditTable(
auto__model=Album,
columns__name__field__include=True,
columns__year__field__include=True,
create_form__title='Add an album',
)
How do I customize the Save and Add row actions?¶
The buttons below an EditTable (save and add_row) live in the edit_actions namespace.
You can rename, restyle, hide or add buttons via this namespace. For example, to hide the “Add row”
button so users can only edit existing rows:
edit_table = EditTable(
auto__model=Album,
columns__name__field__include=True,
edit_actions__add_row__include=False,
)
How do I set defaults on newly added rows?¶
When a user adds a row, iommi creates a blank instance for it. Use
preprocess_row_for_create to fill in defaults on that instance, for example a
foreign key that isn’t one of the editable columns:
def preprocess_row_for_create(row, **_):
row.artist = black_sabbath
return row
edit_table = EditTable(
auto__model=Album,
columns__name__field__include=True,
columns__year__field__include=True,
preprocess_row_for_create=preprocess_row_for_create,
)
How do I nest an edit table inside a form?¶
Declare an EditTable as an attribute of a Form and it becomes a nested form.
iommi sets parent_form on the edit table for you, and saving the outer form
(with the save_nested_forms post handler) saves the edit table too:
from iommi.form import save_nested_forms
class AlbumsTable(EditTable):
class Meta:
auto__model = Album
auto__include = ['name', 'year']
columns__name__field__include = True
columns__year__field__include = True
class MyForm(Form):
albums = AlbumsTable()
class Meta:
actions__submit__post_handler = save_nested_forms