1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
|
API Reference
=============
`.Accessor` (`.A`)
------------------
.. autoclass:: django_tables2.utils.Accessor
`.RequestConfig`
----------------
.. autoclass:: django_tables2.config.RequestConfig
`.Table`
--------
.. autoclass:: django_tables2.tables.Table
:members: paginate, as_html
`.Table.Meta`
-------------
.. class:: Table.Meta
Provides a way to define *global* settings for table, as opposed to
defining them for each instance.
.. attribute:: attrs
Allows custom HTML attributes to be specified which will be added to
the ``<table>`` tag of any table rendered via
:meth:`.Table.as_html` or the
:ref:`template-tags.render_table` template tag.
:type: `dict`
:default: ``{}``
This is typically used to enable a theme for a table (which is done by
adding a CSS class to the ``<table>`` element). i.e.::
class SimpleTable(tables.Table):
name = tables.Column()
class Meta:
attrs = {'class': 'paleblue'}
.. versionadded:: 0.15.0
It's possible to use callables to create *dynamic* values. A few caveats:
- It's not supported for ``dict`` keys, i.e. only values.
- All values will be resolved on table instantiation.
Consider this example where a unique ``id`` is given to each instance
of the table::
import itertools
counter = itertools.count()
class UniqueIdTable(tables.Table):
name = tables.Column()
class Meta:
attrs = {'id': lambda: 'table_%d' % next(counter)}
.. note::
This functionality is also available via the ``attrs`` keyword
argument to a table's constructor.
.. attribute:: row_attrs
Allows custom HTML attributes to be specified which will be added to
the ``<tr>`` tag of any table rendered.
:type: `dict`
:default: ``{}``
This can be used to add each record's primary key to each row.::
class PersonTable(tables.Table):
class Meta:
model = Person
row_attrs = {'data-id': lambda record: record.pk}
.. versionadded:: 1.2.0
.. attribute:: empty_text
Defines the text to display when the table has no rows.
:type: `unicode`
:default: `None`
If the table is empty and ``bool(empty_text)`` is `True`, a row is
displayed containing ``empty_text``. This is allows a message such as
*There are currently no FOO.* to be displayed.
.. note::
This functionality is also available via the ``empty_text`` keyword
argument to a table's constructor.
.. attribute:: show_header
Defines whether the table header (``<thead>``) should be displayed or
not.
:type: `bool`
:default: `True`
.. note::
This functionality is also available via the ``show_header``
keyword argument to a table's constructor.
.. attribute:: exclude
Defines which columns should be excluded from the table. This is useful
in subclasses to exclude columns in a parent.
:type: tuple of `unicode`
:default: ``()``
Example::
>>> class Person(tables.Table):
... first_name = tables.Column()
... last_name = tables.Column()
...
>>> Person.base_columns
{'first_name': <django_tables2.columns.Column object at 0x10046df10>,
'last_name': <django_tables2.columns.Column object at 0x10046d8d0>}
>>> class ForgetfulPerson(Person):
... class Meta:
... exclude = ('last_name', )
...
>>> ForgetfulPerson.base_columns
{'first_name': <django_tables2.columns.Column object at 0x10046df10>}
.. note::
This functionality is also available via the ``exclude`` keyword
argument to a table's constructor.
However, unlike some of the other `.Table.Meta` options, providing the
``exclude`` keyword to a table's constructor **won't override** the
`.Meta.exclude`. Instead, it will be effectively be *added*
to it. i.e. you can't use the constructor's ``exclude`` argument to
*undo* an exclusion.
.. attribute:: fields
Used in conjunction with `~.Table.Meta.model`, specifies which fields
should have columns in the table.
:type: tuple of `unicode` or `None`
:default: `None`
If `None`, all fields are used, otherwise only those named.
Example::
# models.py
class Person(models.Model):
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
# tables.py
class PersonTable(tables.Table):
class Meta:
model = Person
fields = ('first_name', )
.. attribute:: model
A model to inspect and automatically create corresponding columns.
:type: Django model
:default: `None`
This option allows a Django model to be specified to cause the table to
automatically generate columns that correspond to the fields in a
model.
.. attribute:: order_by
The default ordering. e.g. ``('name', '-age')``. A hyphen ``-`` can be
used to prefix a column name to indicate *descending* order.
:type: `tuple`
:default: ``()``
.. note::
This functionality is also available via the ``order_by`` keyword
argument to a table's constructor.
.. attribute:: sequence
The sequence of the table columns. This allows the default order of
columns (the order they were defined in the Table) to be overridden.
:type: any iterable (e.g. `tuple` or `list`)
:default: ``()``
The special item ``'...'`` can be used as a placeholder that will be
replaced with all the columns that weren't explicitly listed. This
allows you to add columns to the front or back when using inheritance.
Example::
>>> class Person(tables.Table):
... first_name = tables.Column()
... last_name = tables.Column()
...
... class Meta:
... sequence = ('last_name', '...')
...
>>> Person.base_columns.keys()
['last_name', 'first_name']
The ``"..."`` item can be used at most once in the sequence value. If
it's not used, every column *must* be explicitly included. e.g. in the
above example, ``sequence = ("last_name", )`` would be **invalid**
because neither ``"..."`` or ``"first_name"`` were included.
.. note::
This functionality is also available via the ``sequence`` keyword
argument to a table's constructor.
.. attribute:: orderable
Default value for column's *orderable* attribute.
:type: `bool`
:default: `True`
If the table and column don't specify a value, a column's ``orderable``
value will fallback to this. This provides an easy mechanism to disable
ordering on an entire table, without adding ``orderable=False`` to each
column in a table.
.. note::
This functionality is also available via the ``orderable`` keyword
argument to a table's constructor.
.. attribute:: template
The default template to use when rendering the table.
:type: `unicode`
:default: ``"django_tables2/table.html"``
.. note::
This functionality is also available via the *template* keyword
argument to a table's constructor.
.. attribute:: localize
Specifies which fields should be localized in the table.
Read :ref:`localization-control` for more information.
:type: tuple of `unicode`
:default: empty tuple
.. attribute:: unlocalize
Specifies which fields should be unlocalized in the table.
Read :ref:`localization-control` for more information.
:type: tuple of `unicode`
:default: empty tuple
`.BooleanColumn`
----------------
.. autoclass:: django_tables2.columns.BooleanColumn
`.Column`
---------
.. autoclass:: django_tables2.columns.Column
`.CheckBoxColumn`
-----------------
.. autoclass:: django_tables2.columns.CheckBoxColumn
:members:
`.DateColumn`
-------------
.. autoclass:: django_tables2.columns.DateColumn
:members:
`.DateTimeColumn`
-----------------
.. autoclass:: django_tables2.columns.DateTimeColumn
:members:
`.EmailColumn`
--------------
.. autoclass:: django_tables2.columns.EmailColumn
:members:
`.FileColumn`
-------------
.. autoclass:: django_tables2.columns.FileColumn
:members:
`.LinkColumn`
-------------
.. autoclass:: django_tables2.columns.LinkColumn
:members:
`.RelatedLinkColumn`
--------------------
.. autoclass:: django_tables2.columns.RelatedLinkColumn
:members:
`.TemplateColumn`
-----------------
.. autoclass:: django_tables2.columns.TemplateColumn
:members:
`.URLColumn`
------------
.. autoclass:: django_tables2.columns.URLColumn
:members:
See :doc:`internal` for internal classes.
|