from django.template.loader import get_template
from django.template import Context
from django.utils.encoding import StrAndUnicode
+from django.db.models.query import QuerySet
from .utils import OrderBy, OrderByTuple, Accessor, AttributeDict
from .rows import BoundRows, BoundRow
from .columns import BoundColumns, Column
This class is used by :class:`.Table` to wrap any
input table data.
"""
-
def __init__(self, data, table):
- from django.db.models.query import QuerySet
if isinstance(data, QuerySet):
self.queryset = data
elif isinstance(data, list):
TableDataClass = TableData
def __init__(self, data, order_by=None, sortable=None, empty_text=None,
- exclude=None):
+ exclude=None, attrs=None):
self._rows = BoundRows(self)
self._columns = BoundColumns(self)
self._data = self.TableDataClass(data=data, table=self)
+ self.attrs = attrs
self.empty_text = empty_text
self.sortable = sortable
if order_by is None:
:rtype: :class:`~.utils.AttributeDict` object.
"""
- return self._meta.attrs
+ return self._attrs if self._attrs is not None else self._meta.attrs
+
+ @attrs.setter
+ def attrs(self, value):
+ self._attrs = value
def paginate(self, klass=Paginator, per_page=25, page=1, *args, **kwargs):
self.paginator = klass(self.rows, per_page, *args, **kwargs)
Default: ``{}``
+ .. note::
+
+ This functionality is also available via the ``attrs`` keyword
+ argument to a table's constructor.
+
.. attribute:: empty_text
Defines the text to display when the table has no rows.
+ .. note::
+
+ This functionality is also available via the ``empty_text`` keyword
+ argument to a table's constructor.
+
.. attribute:: exclude
Defines which columns should be excluded from the table. This is useful
Default: ``()``
+ .. note::
+
+ This functionality is also available via the ``exclude`` keyword
+ argument to a table's constructor.
+
.. attribute:: order_by
The default ordering. e.g. ``('name', '-age')``. A hyphen ``-`` can be
Default: ``()``
+ .. note::
+
+ This functionality is also available via the ``order_by`` keyword
+ argument to a table's constructor.
+
.. attribute:: sortable
The default value for determining if a :class:`.Column` is sortable.
Default: :const:`True`
+ .. note::
+
+ This functionality is also available via the ``sortable`` keyword
+ argument to a table's constructor.
+
:class:`TableData` Objects:
------------------------------
assert 'added' in CityTable.base_columns
+@core.test
+def attrs():
+ class TestTable(tables.Table):
+ class Meta:
+ attrs = {}
+ Assert({}) == TestTable([]).attrs
+
+ class TestTable2(tables.Table):
+ class Meta:
+ attrs = {"a": "b"}
+ Assert({"a": "b"}) == TestTable2([]).attrs
+
+ class TestTable3(tables.Table):
+ pass
+ Assert({}) == TestTable3([]).attrs
+ Assert({"a": "b"}) == TestTable3([], attrs={"a": "b"}).attrs
+
+ class TestTable4(tables.Table):
+ class Meta:
+ attrs = {"a": "b"}
+ Assert({"c": "d"}) == TestTable4([], attrs={"c": "d"}).attrs
+
+
@core.test
def datasource_untouched():
"""Ensure that data that is provided to the table (the datasource) is not