Edit tables
-----------
.. _edit-table-one-to-one:
How do you edit one-to-one fields in an edit table?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. uses EditColumn.field
.. uses EditColumn.columns
.. uses EditColumn.auto
Include them in `auto__include`. Say you have a profile model for an artist:
Then you can include the artist name field:
.. code-block:: python
edit_table = EditTable(
auto__model=Profile,
auto__include=['artist__name'],
columns__artist_name__field__include=True,
)
.. raw:: html
▼ Hide result
Toggle structure
.. _edit-table-delete-as-checkbox:
How do I change the delete buttons to checkboxes?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Just add `data-iommi-edit-table-delete-with="checkbox"`:
.. code-block:: python
edit_table = EditTable(
auto__model=Profile,
auto__include=['artist__name'],
columns__artist_name__field__include=True,
columns__delete=EditColumn.delete(),
**{
'attrs__data-iommi-edit-table-delete-with': 'checkbox',
}
)
.. raw:: html
▼ Hide result
Toggle structure
.. _edit-table-include-field-labels:
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`:
.. code-block:: python
edit_table = EditTable(
auto__model=Profile,
auto__include=['artist__name'],
columns__artist_name__field__include=True,
extra_evaluated__input_labels_include=True,
)
.. raw:: html
▼ Hide result
Toggle structure
.. _edit-table-reorderable:
How do I let users drag to reorder rows?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. uses EditTable.reorderable
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`:
.. code-block:: python
edit_table = EditTable(
auto__model=FavoriteArtist,
auto__include=['artist__name', 'comment', 'sort_order'],
columns__comment__field__include=True,
reorderable=True,
sortable=False,
)
.. raw:: html
▼ Hide result
Toggle structure
.. _edit-table-forms:
How do I configure the edit and create forms of an edit table?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. uses EditTable.edit_form
.. uses EditTable.create_form
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:
.. code-block:: python
edit_table = EditTable(
auto__model=Album,
columns__name__field__include=True,
columns__year__field__include=True,
create_form__title='Add an album',
)
.. raw:: html
▼ Hide result
Toggle structure
.. _edit-table-actions:
How do I customize the Save and Add row actions?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. uses EditTable.edit_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:
.. code-block:: python
edit_table = EditTable(
auto__model=Album,
columns__name__field__include=True,
edit_actions__add_row__include=False,
)
.. raw:: html
▼ Hide result
Toggle structure
.. _edit-table-preprocess-create:
How do I set defaults on newly added rows?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. uses EditTable.preprocess_row_for_create
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:
.. code-block:: python
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,
)
.. raw:: html
▼ Hide result
Toggle structure
.. _edit-table-parent-form:
How do I nest an edit table inside a form?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. uses EditTable.parent_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:
.. code-block:: python
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
.. raw:: html
▼ Hide result
Toggle structure