"""\r
def __init__(self, data=None, *args, **kwargs):\r
if data == None:\r
+ if self._meta.model is None:\r
+ raise ValueError('Table without a model association needs '\r
+ 'to be initialized with data')\r
self.queryset = self._meta.model._default_manager.all()\r
- elif isinstance(data, models.Model):\r
+ elif hasattr(data, '_default_manager'): # saves us db.models import\r
self.queryset = data._default_manager.all()\r
else:\r
self.queryset = data\r
def test_basic():\r
"""Some tests here are copied from ``test_basic.py`` but need to be\r
rerun with a ModelTable, as the implementation is different."""\r
+\r
class CountryTable(tables.ModelTable):\r
null = tables.Column(default="foo")\r
tld = tables.Column(name="domain")\r
exclude = ('id',)\r
countries = CountryTable()\r
\r
- for r in countries.rows:\r
- # "normal" fields exist\r
- assert 'name' in r\r
- # unknown fields are removed/not accessible\r
- assert not 'does-not-exist' in r\r
- # ...so are excluded fields\r
- assert not 'id' in r\r
- # missing data is available with default values\r
- assert 'null' in r\r
- assert r['null'] == "foo" # note: different from prev. line!\r
-\r
- # all that still works when name overrides are used\r
- assert not 'tld' in r\r
- assert 'domain' in r\r
- assert len(r['domain']) == 2 # valid country tld\r
+ def test_country_table(table):\r
+ for r in table.rows:\r
+ # "normal" fields exist\r
+ assert 'name' in r\r
+ # unknown fields are removed/not accessible\r
+ assert not 'does-not-exist' in r\r
+ # ...so are excluded fields\r
+ assert not 'id' in r\r
+ # missing data is available with default values\r
+ assert 'null' in r\r
+ assert r['null'] == "foo" # note: different from prev. line!\r
+\r
+ # all that still works when name overrides are used\r
+ assert not 'tld' in r\r
+ assert 'domain' in r\r
+ assert len(r['domain']) == 2 # valid country tld\r
+ test_country_table(countries)\r
+\r
+ # repeat the avove tests with a table that is not associated with a\r
+ # model, and all columns being created manually.\r
+ class CountryTable(tables.ModelTable):\r
+ name = tables.Column()\r
+ population = tables.Column()\r
+ capital = tables.Column()\r
+ system = tables.Column()\r
+ null = tables.Column(default="foo")\r
+ tld = tables.Column(name="domain")\r
+ countries = CountryTable(Country)\r
+ test_country_table(countries)\r
\r
# make sure the row and column caches work for model tables as well\r
assert id(list(countries.columns)[0]) == id(list(countries.columns)[0])\r
# TODO: pagination\r
# TODO: support function column sources both for modeltables (methods on model) and static tables (functions in dict)\r
# TODO: manual base columns change -> update() call (add as example in docstr here) -> rebuild snapshot: is row cache, column cache etc. reset?\r
-# TODO: throw an exception on invalid order_by\r
-# TODO: option to skip model table generation (leave off model option?)
\ No newline at end of file
+# TODO: throw an exception on invalid order_by
\ No newline at end of file