datatables
¶
Server-side Datatables are Form-like classes that are responsible for processing ajax queries from the client. A Datatable is referenced by a view, and the view initializes the Datatable with the original queryset. The Datatable is responsible for filtering and sorting the results, and the final object list is handed back to the view for serialization.
A Datatable
, like a ModelForm
, should contain an inner Meta
class that can declare various options for importing model fields as columns, setting the verbose names, etc.
Datatable¶
ValuesDatatable¶
Meta
class and options¶
-
class
Meta
¶ -
model
¶ Default: queryset.model
The model class represented by the table.
-
columns
¶ Default: All local non-relationship model fields. The list of local model fields to be imported from the base model. The appropriate
Column
will be generated for each. Relationship-spanning ORM paths should not be used here, nor any “virtual” data getter like a method or property. For those, you should instead declare an explicit column on theDatatable
with a name of your choosing, and set thesources
accordingly.
-
exclude
¶ Default: []
A list of model field names to exclude if
columns
is not given.
-
cache_type
¶ Default: None
The identifier for caching strategy to use on the
object_list
sent to the datatable. See Caching for more information.
-
ordering
¶ Default: The model
‘sMeta.ordering
option.A list that controls the default table sorting, giving column names in the order of their sort priority. When a Column name is given instead of a model field name, that column’s
sources
list will be looked up for any sortable fields it references.As with model ordering, using a
-
prefix in front of a name will reverse the order.
-
page_length
¶ Default: 25
The default page length for response results. This can be changed by the user, and is ultimately in the hands of the client-side JS to configure.
-
search_fields
¶ Default: []
A list of extra query paths to use when performing searches. This is useful to reveal results that for data points that might not be in the table, but which the user might intuitively expect a match.
Example: ['house__city__abbreviation]
-
unsortable_columns
¶ Default: []
A list of model fields from
columns
that should not be sortable when theirColumn
instances are created. Explicitly declared columns should sendsortable=False
instead of listing the column here.
Default: []
A list of column names that will be transmitted during ajax requests, but which the client should hide from the table by default. Using this setting does not enhance performance. It is purely for datatable export modes to use as a hint.
-
structure_template
¶ Default: 'datatableview/default_structure.html'
The template that will be rendered when the
Datatable
instance is coerced to a string (when the datatable is printed out in a template). The template serves as the starting point for the client-side javascript to initialize.The default template creates
<th>
headers that have adata-name
attribute that is the slug of the column name for easy CSS targeting, and the default search and sort options that thedatatableview.js
initializer will read to build initialization options.
Default: False
Controls the existence of a
<tfoot>
element in the table. IfTrue
, the defaultstructure_template
will render another set of<th>
elements with appropriate labels.This is particularly useful when setting up something like per-column searching, which officially leverages the table footer, replacing each simple footer text label with a search box that applies only to that column’s content.
-
result_counter_id
¶ Default: 'id_count'
A helper setting that names a CSS
id
that thedatatableview.js
initializer will configure to hold a total result counter. This is strictly in addition to the normal readout that appears under a datatable. If you don’t want any such external result display, you can ignore this setting.
-
labels
¶ Default: {}
A dict of model field names from
columns
that should have theirverbose_name
setting overridden for the table header.Example: labels = {'name': "Headline"}
-
processors = None
Default: {}
A dict of model field names from
columns
that need to declare aprocessor
callback. The mapped values may be direct references to callables, or strings that name a method on the Datatable or view.Example: processors = {'name': 'get_name_data'}
-