Configure the gridΒΆ

Grid columns can be configured using the _list_fields property of the views class, which is an ordered list of ListField objects, one for each column.

The ListField constructor take some parameters:

  • model: the SQLAlchemy mapper (required if attr is an attribute name).
  • attr: the model attribute name to use or an SQLAlchemy InstrumentedAttribute.
  • key: an identifier for the column, default to attribute.key.
  • label: text for the column header, default to colanderalchemy title for the field.
  • renderer: callable that takes an entity of the SQLAlchemy mapper and returns a string value.
  • sort_column: An IntrumentedAttribute to use in sort_by.
  • filter_column: An IntrumentedAttribute to filter with.
  • visible: a boolean for the initial visible state of this column.

Every time the table index page asks for data from the grid view, the AbstractView will create a default query using AbstractViews._base_query method.

If you use columns coming from relationships, this might result in sending one request to the database for each relationship and each record. In such cases, you should override the _base_query method to use eager loading for those relationships, for example:

def _base_query(self):
    return self._request.dbsession.query(Excavation).distinct(). \
        join('situations'). \
        options(subqueryload('situations'))

Note that you also need to join the relationships you use for sorting and filtering.