Skip to content

Commit

Permalink
Add example on how to use Accessor to documentation in `custom-data…
Browse files Browse the repository at this point in the history
….rst` (#917)

Accessor is an excellent feature of Django Tables2 but it is not highlighted enough due to lack of documentation. I am adding an example how it can be used with `table.Column` class.
  • Loading branch information
ruddra committed Oct 18, 2023
1 parent 44fce19 commit 2318bc1
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/pages/custom-data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ The separators ``__`` represent relationships, and are attempted in this order:

If the resulting value is callable, it is called and the return value is used.

Moreover, you can use `accessor` with the table columns to access nested values. For example::

>>> data = [{"abc": {"one": {"two": "three"}}}, {"abc": {"one": {"two": "four"}}}]
>>> class MyTable(tables.Table):
... abc = tables.Column(accessor="abc__one__two")
>>> table = MyTable(data)
>>> list(map(str, table.rows[1]))
['four']

.. _table.render_foo:

`Table.render_foo` methods
Expand Down

0 comments on commit 2318bc1

Please sign in to comment.