You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jul 15, 2025. It is now read-only.
This is the basic usage of the table sort. You can use this to display Queryset and also list of items.
36
+
This is the basic usage of the table sort. You can use this to display a Queryset and also a list of items.
38
37
39
38
.. note::
40
-
The default text for the header when displaying data from a Queryset is the verbose_name of the field. For list of any other object you must set the header text using the column_names parameter.
39
+
40
+
The default text for the header when displaying data from a Queryset is the verbose_name of the field. For a list of any other object, you must set the header text using the column_names parameter.
41
41
42
42
Table CSS
43
43
*********
44
44
45
-
You can provide the css classes that the table should have as below.
45
+
You can provide the CSS classes that the table should have as shown below.
46
46
47
47
.. code-block:: python
48
48
@@ -63,49 +63,49 @@ You can provide the css classes that the table should have as below.
The default behavior is to show all fields of the model. If you want to show only certain field you can set this in the fields parameter as follows.
74
+
The default behavior is to show all fields of the model. If you want to show only certain fields, you can set this in the fields parameter as follows.
75
75
76
76
.. code-block:: python
77
77
78
78
TableSort(request, object_list, fields=["name"])
79
79
80
-
This code will display only the field name in the table. You can also set which fields you don't want to display.
80
+
This code will display only the field "name" in the table. You can also set which fields you don't want to display.
81
81
82
82
.. code-block:: python
83
83
84
84
TableSort(request, object_list, exclude=["age"])
85
85
86
-
Any field you pass in the exclude parameter will not be display, and the others that aren't, will be.
86
+
Any field you pass in the exclude parameter will not be displayed, and the others that aren't will be.
87
87
88
88
.. warning::
89
-
The current implementation looks first for the exclude field. So if you provide both fields and exclude, all the field no matter if is in the list of field you declared in the fields parameter **will not be displayed**.
90
89
90
+
The current implementation looks for the exclude field first. So if you provide both fields and exclude, all the fields, no matter if they are in the list of fields you declared in the fields parameter, **will not be displayed**.
You can set a custom header for any field. For this you can use the column_names parameter.
99
+
You can set a custom header for any field. For this, you can use the column_names parameter.
100
100
101
101
.. warning::
102
-
If you set the fields and exclude parameter to None, and you provide the column_names
103
-
parameter, all the fields that are given will be displayed.
104
102
105
-
Adding extra columns
103
+
If you set the fields and exclude parameters to None and you provide the column_names parameter, all the fields that are given will be displayed.
104
+
105
+
Adding Extra Columns
106
106
********************
107
107
108
-
Sometimes you may want to add a custom column to the table column. You can do this using the added_columns parameter.
108
+
Sometimes you may want to add a custom column to the table. You can do this using the added_columns parameter.
109
109
110
110
.. code-block:: python
111
111
@@ -121,13 +121,12 @@ Sometimes you may want to add a custom column to the table column. You can do th
121
121
added_columns=[(("added_column_1", "Sum"), sum)],
122
122
)
123
123
124
-
The added_columns takes a list of tuples, following this pattern ((field_identifier, field_header), callable_function). The field_identifier is a str value to identify the field, the field_header to set the text of the header and the callable_function should be a function that takes one parameter and return a string value. The callable_function will be called for each row and the object that should be displayed is passed to as a parameter to the function.
124
+
The added_columns parameter takes a list of tuples following this pattern: ((field_identifier, field_header), callable_function). The field_identifier is a string value to identify the field, the field_header is used to set the text of the header, and the callable_function should be a function that takes one parameter and returns a string value. The callable_function will be called for each row, and the object that should be displayed is passed as a parameter to the function.
125
125
126
-
127
-
List of items
126
+
List of Items
128
127
*************
129
128
130
-
For list of items you need to set the column_names. All the field in the dictionary will be displayed.
129
+
For a list of items, you need to set the column_names. All the fields in the dictionary will be displayed.
131
130
132
131
.. code-block:: python
133
132
@@ -139,12 +138,13 @@ For list of items you need to set the column_names. All the field in the diction
139
138
)
140
139
141
140
.. note::
142
-
You can use the added_columns parameter to add other custom columns the same.
143
141
144
-
Primary key
142
+
You can use the added_columns parameter to add other custom columns in the same way.
143
+
144
+
Primary Key
145
145
***********
146
146
147
-
Sometimes you may want to show the primary key of your model, the default behavior is not to display the primary key of a Queryset since most of the time it is not useful to you this to the user.
147
+
Sometimes you may want to show the primary key of your model. The default behavior is not to display the primary key of a Queryset since it is often not useful to show this to the user.
148
148
149
149
.. code-block:: python
150
150
@@ -154,11 +154,10 @@ Sometimes you may want to show the primary key of your model, the default behavi
154
154
show_primary_key=True,
155
155
)
156
156
157
-
158
-
Fields order
157
+
Fields Order
159
158
************
160
159
161
-
You can set the order to display the field in the table. For this you should use the field_order parameter.
160
+
You can set the order to display the fields in the table. For this, you should usethe field_order parameter.
162
161
163
162
.. code-block:: python
164
163
@@ -168,10 +167,28 @@ You can set the order to display the field in the table. For this you should use
168
167
field_order=["age"],
169
168
)
170
169
171
-
This will display the age as the first column in the table.
170
+
This will display the "age" as the first column in the table.
172
171
173
172
.. note::
174
-
The field will be displayed following the order you give, but if you don't include a given field this
175
-
will be displayed as the last. The field_order parameter works as a priority list.
176
173
177
-
To see the different options you can provide please see the section :ref:`table-sort-class`.
174
+
The fields will be displayed following the order you give, but if you don't include a given field, it will be displayed as the last. The field_order parameter works as a priority list.
175
+
176
+
177
+
Customizing the Table Template
178
+
****************************
179
+
180
+
You can customize the template used for generating the table by providing a different `template_name` parameter in the TableSort constructor. By default, the template used is `'django_table_sort/table.html'`. Here's an example:
181
+
182
+
.. code-block:: python
183
+
184
+
TableSort(
185
+
request,
186
+
object_list,
187
+
template_name="custom_template.html",
188
+
)
189
+
190
+
In the above example, the `'custom_template.html'` file will be used instead of the default template for generating the table.
191
+
192
+
To create your custom template, you can copy the contents of the default template `'django_table_sort/table.html'` and modify it according to your needs.
193
+
194
+
To see the different options you can provide, please see the section :ref:`table-sort-class`.
0 commit comments