Base Model

We extends the peewee.Model class to integrate with the server-side processing logic of DataTables. Some monkeypathes were made as certain functionalities with peewee are not correctly implemented.

class flask_datatables.model.Model(*args, **kwargs)[source]

Bases: Model

Extends peewee.Model with DataTables support.

To ease the Model class from recursively processing its field data, we added the following two attributes to save the DataTables integrated fields data.

dt_orderable: Dict[str, Field] = {}

DataTables orderable fields.

dt_searchable: Dict[str, Field] = {}

DataTables searchable fields.

classmethod validate_model()[source]

Validates data model and dynamically insert fields.

If DataTables integration is enabled for the data model, this method will insert fields (database columns) for both order and search operations respectively on each defined fields according to the original field type definition.

By default, each field is orderable and/or searchable as long as the datatables switch is enabled. When the orderable and/or searchable attributes are set to an instance of a Field, Flask-DataTables will insert additional fields of such type with _dt_order and/or _dt_search suffix as the field names accordingly.

Return type:


save(force_insert=False, only=None)[source]

Save the data in the model instance.

The method extends the original method by automatically update the searching and ordering field data with the actual data.

  • force_insert (bool) – Force INSERT query.

  • only (Optional[List[Field]]) – Only save the given Field instances.

Return type:



Number of rows modified.

classmethod search(query=None, factory=None)[source]

Server-side processing integration with DataTables.

Return type:



Selected information from the database in format to be sent to DataTables.

See also

The factory function takes exactly one parameter, the data record returned from peewee selection, and returns the converted data of fields. See flask_datatables.utils.prepare_response() for an example.

class flask_datatables.model.Metadata(model, database=None, table_name=None, indexes=None, primary_key=None, constraints=None, schema=None, only_save_dirty=False, depends_on=None, options=None, db_table=None, table_function=None, table_settings=None, without_rowid=False, temporary=False, strict_tables=None, legacy_table_names=True, **kwargs)[source]

Bases: Metadata

Basic metadata for data models.

Flask-DataTables extends the original metadata record from peewee with a datatables switch to indicate if current data model supports and/or enables DataTables server-side processing integration.

datatables: bool = False

DataTables integration indicator flag.