Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made as_values method from TableBase an interator #432

Merged
merged 1 commit into from
Apr 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions django_tables2/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,13 +556,13 @@ def as_html(self, request):

def as_values(self):
'''
Return a 2d array of the data which would be shown in the table where the first row is the table headers.
Return a row iterator of the data which would be shown in the table where the first row is the table headers.

This can be used to output the table data as CSV, excel, etc
'''
headings = [str(c.header) for c in self.columns]
rows = [[r.get_cell_value(column.name) for column in r.table.columns] for r in self.rows]
return [headings] + rows
yield [str(c.header) for c in self.columns]
for r in self.rows:
yield [r.get_cell_value(column.name) for column in r.table.columns]

def has_footer(self):
'''
Expand Down
4 changes: 2 additions & 2 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ class Table(tables.Table):
expected = [['Name', 'Country']] + [[r['name'], r['country']] for r in data]
table = Table(data)

assert table.as_values() == expected
assert list(table.as_values()) == expected


def test_as_values_empty_values():
Expand All @@ -542,7 +542,7 @@ class Table(tables.Table):
]
expected = [['Name', 'Country']] + [[r.get('name'), r.get('country')] for r in data]
table = Table(data)
assert table.as_values() == expected
assert list(table.as_values()) == expected


def test_row_attrs():
Expand Down
2 changes: 1 addition & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,4 +421,4 @@ class Meta:
table = PersonTable(Person.objects.all())

with assertNumQueries(1):
table.as_values()
list(table.as_values())