From 1dc4a1ddfc6580a688a0b78e32a997b3546d8ca7 Mon Sep 17 00:00:00 2001 From: peio Date: Fri, 7 Apr 2017 11:25:37 +0200 Subject: [PATCH] Made as_values method from TableBase an interator --- django_tables2/tables.py | 8 ++++---- tests/test_core.py | 4 ++-- tests/test_models.py | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/django_tables2/tables.py b/django_tables2/tables.py index 8c227491..bd1d6d8c 100644 --- a/django_tables2/tables.py +++ b/django_tables2/tables.py @@ -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): ''' diff --git a/tests/test_core.py b/tests/test_core.py index 27efd4ac..f8437222 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -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(): @@ -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(): diff --git a/tests/test_models.py b/tests/test_models.py index 1bbc685a..21b68c3f 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -421,4 +421,4 @@ class Meta: table = PersonTable(Person.objects.all()) with assertNumQueries(1): - table.as_values() + list(table.as_values())