-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added optional
Table
argument to add extra columns to an instance.
- Loading branch information
Showing
4 changed files
with
43 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,33 @@ | ||
# coding: utf-8 | ||
from __future__ import absolute_import, unicode_literals | ||
|
||
import pytest | ||
|
||
import django_tables2 as tables | ||
|
||
|
||
@pytest.mark.skip('not yet fixed, issue #403') | ||
def test_dynamically_adding_columns(): | ||
class Table(tables.Table): | ||
''' | ||
This table allows adding columns while initializing the table. | ||
''' | ||
name = tables.Column() | ||
|
||
def __init__(self, data, extra_columns=None, *args, **kwargs): | ||
''' | ||
Pass in a list of tuples of extra columns to add in the | ||
format (colunm_name, column) | ||
''' | ||
if extra_columns: | ||
for col_name, col in extra_columns: | ||
self.base_columns[col_name] = col | ||
super(Table, self).__init__(data, *args, **kwargs) | ||
''' | ||
When adding columns to self.base_columns, they are actually added to | ||
the class attribute `Table.base_columns`, and not to the instance | ||
attribute, `table.base_columns` | ||
issue #403 | ||
''' | ||
data = [ | ||
{'name': 'Adrian', 'country': 'Australia'}, | ||
{'name': 'Adrian', 'country': 'Brazil'}, | ||
{'name': 'Audrey', 'country': 'Chile'}, | ||
{'name': 'Bassie', 'country': 'Belgium'}, | ||
] | ||
table = Table(data, extra_columns=[('country', tables.Column())]) | ||
assert table.columns.columns.keys() == ['name', 'country'] | ||
|
||
# a new instance should not have the extra columns added to the | ||
# first instance. | ||
table2 = Table(data) | ||
assert table2.columns.columns.keys() == ['name'] | ||
class MyTable(tables.Table): | ||
name = tables.Column() | ||
|
||
# this is obvious: | ||
assert list(MyTable(data).columns.columns.keys()) == ['name'] | ||
|
||
assert list(MyTable(data, extra_columns=[ | ||
('country', tables.Column()) | ||
]).columns.columns.keys()) == ['name', 'country'] | ||
|
||
# this new instance should not have the extra columns added to the first instance. | ||
assert list(MyTable(data).columns.columns.keys()) == ['name'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
817d711
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Thanks @jieter - looks good