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.modelThe 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
Columnwill 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 theDatatablewith a name of your choosing, and set thesourcesaccordingly.
-
exclude¶ Default: []A list of model field names to exclude if
columnsis not given.
-
cache_type¶ Default: NoneThe identifier for caching strategy to use on the
object_listsent to the datatable. See Caching for more information.
-
ordering¶ Default: The model‘sMeta.orderingoption.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
sourceslist 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: 25The 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
columnsthat should not be sortable when theirColumninstances are created. Explicitly declared columns should sendsortable=Falseinstead 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
Datatableinstance 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-nameattribute that is the slug of the column name for easy CSS targeting, and the default search and sort options that thedatatableview.jsinitializer will read to build initialization options.
Default: FalseControls the existence of a
<tfoot>element in the table. IfTrue, the defaultstructure_templatewill 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
idthat thedatatableview.jsinitializer 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
columnsthat should have theirverbose_namesetting overridden for the table header.Example: labels = {'name': "Headline"}
-
processors = None Default: {}A dict of model field names from
columnsthat need to declare aprocessorcallback. 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'}
-