DataTables
This module covers a class to support JQuery DataTable within the REST
API of this project. It is recommended to use apply(...)
to filter
a specific queryset.
Important
All list views of the REST API support jQuery DataTable requests, so sorting, filtering and search will be applied to all of them.
- mastf.MASTF.utils.datatable.apply(request: DataTableRequest, queryset: QuerySet) QuerySet [source]
Utility function that applies filters or ordering to a Django queryset based on a
DataTableRequest
object.This function can be used in conjunction with Django’s generic views and the DataTables jQuery plugin to create dynamic data tables with server-side filtering, sorting, and pagination. Simply pass the DataTableRequest object and the queryset to this function in your view’s
get_queryset(...)
method, and return the result (or a pageinated one).For example, to use this function with the Django ListView, you could define your view like this:
1from django.views.generic import ListView 2from mastf.MASTF.models import MyModel 3from mastf.MASTF.utils import datatable 4 5class MyListView(ListView): 6 model = MyModel 7 template_name = "my_template.html" 8 9 def get_queryset(self): 10 request = datatable.DataTableRequest(self.request) 11 queryset = super().get_queryset() 12 return datatable.apply(request, queryset)
To use your defined view within a jQuery DataTable, you should set the following parameters:
var options = { "processing": true, "serverSide": true, "ajax": { // assuming the view is mapped to an URL path with name=MyListView "url": "{% url 'MyListView' %}", "dataSrc": function(json) { return json.results; }, }, "columns": { {"data": "mycolumn"}, // ... } }; $(element).DataTable(options);
- Parameters:
request (
DataTableRequest
) – aDataTableRequest
object containing information about the current data table view, such as search keywords, sorting column, and pagination.queryset (QuerySet) – the queryset to apply the filters and ordering to.
- Returns:
A filtered and/or ordered queryset based on the DataTableRequest object.
- Return type:
QuerySet
- class mastf.MASTF.utils.datatable.DataTableRequest(request: HttpRequest)[source]
Parse jQuery DataTables requests.
This class provides a convenient way to extract the necessary data from a jQuery DataTables request to construct a query for the database. It takes the request and creates a list of columns that should be queried/searched:
1from django.http import HttpRequest 2from myapp.models import MyModel 3 4def my_view(request: HttpRequest): 5 dt_request = DataTableRequest(request) 6 # use the extracted data to perform database queries or other# 7 # relevant operations.
In general, the extracted column data will be stored with the following structure:
>>> dt_request = DataTableRequest(request) >>> dt_request.columns [{'name': "Column1", 'params': {...}}, ...]
Note that the params dictionary can be used in Django’s database queries directly by just passing
**column["params"]
.HttpRequest Structure
While this class is capable of parsing DataTable requests, it can be used within every context having the following parameters in mind:
column[$idx][data]
: Stores the column name at the specified indexcolumn[$idx][searchable]
: Indicates whether this column is searchablecolumn[$idx][search][value]
: Specifies an extra search value that should be applied instead of the global one.search[value]
: Global search valueorder[0][column]
: Defines the column that should be ordered in a specific directionorder[0][dir]
: The sorting directionstart
: offset position where to startlength
: preferred data length to return
- property columns: list
Specifies all column data that is present within this request.
- Returns:
a list of column structures.
- Return type:
list
- property length: int
Defines the preferred return size.
- Returns:
an integer or
0
if this parameter is not present.- Return type:
int
- property order_column: int
The column index which points to a column that should be ordered.
- Returns:
-1
if no column is selected ot the column index- Return type:
int
- property order_direction: str
Specifies the order direction.
- Returns:
the direction as string (either
asc
ordesc
)- Return type:
str
- property search_value: str
Defines a global search value
- Returns:
_description_
- Return type:
str
- property start: int
Defines the starting pointer.
- Returns:
an integer pointing to the starting offset position
- Return type:
int