From de94fb244ec96b6bb0c2f4dc42d2d2b92308a2e2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Michael=20Elsd=C3=B6rfer?= Date: Thu, 19 Jun 2008 16:26:16 +0000 Subject: [PATCH] model column auto generation can be disabled now --- README | 4 ++++ django_tables/models.py | 5 ++++- tests/test_basic.py | 4 ++-- tests/test_models.py | 48 ++++++++++++++++++++++++++--------------- 4 files changed, 41 insertions(+), 20 deletions(-) diff --git a/README b/README index 38153ea..2a8c628 100644 --- a/README +++ b/README @@ -140,6 +140,10 @@ If you are using model inheritance, then the following also works: Note that while you can pass any model, it really only makes sense if the model also provides fields for the columns you have defined. +If you just want to use ModelTables, but without auto-generated columns, +you do not have to list all model fields in the ``exclude`` Meta option. +Instead, simply don't specify a model. + Custom Columns ~~~~~~~~~~~~~~ diff --git a/django_tables/models.py b/django_tables/models.py index 1747442..b0587e3 100644 --- a/django_tables/models.py +++ b/django_tables/models.py @@ -71,8 +71,11 @@ class BaseModelTable(BaseTable): """ def __init__(self, data=None, *args, **kwargs): if data == None: + if self._meta.model is None: + raise ValueError('Table without a model association needs ' + 'to be initialized with data') self.queryset = self._meta.model._default_manager.all() - elif isinstance(data, models.Model): + elif hasattr(data, '_default_manager'): # saves us db.models import self.queryset = data._default_manager.all() else: self.queryset = data diff --git a/tests/test_basic.py b/tests/test_basic.py index a3b8295..08dd015 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -68,12 +68,12 @@ def test_basic(): assert not 'id' in r # missing data is available as default assert 'answer' in r - assert r['answer'].value == 42 # note: different from prev. line! + assert r['answer'] == 42 # note: different from prev. line! # all that still works when name overrides are used assert not 'c' in r assert 'count' in r - assert r['count'].value == 1 + assert r['count'] == 1 # changing an instance's base_columns does not change the class assert id(books.base_columns) != id(BookTable.base_columns) diff --git a/tests/test_models.py b/tests/test_models.py index 1f01d83..d04dc19 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -86,6 +86,7 @@ def test_declaration(): def test_basic(): """Some tests here are copied from ``test_basic.py`` but need to be rerun with a ModelTable, as the implementation is different.""" + class CountryTable(tables.ModelTable): null = tables.Column(default="foo") tld = tables.Column(name="domain") @@ -94,21 +95,35 @@ def test_basic(): exclude = ('id',) countries = CountryTable() - for r in countries.rows: - # "normal" fields exist - assert 'name' in r - # unknown fields are removed/not accessible - assert not 'does-not-exist' in r - # ...so are excluded fields - assert not 'id' in r - # missing data is available with default values - assert 'null' in r - assert r['null'] == "foo" # note: different from prev. line! - - # all that still works when name overrides are used - assert not 'tld' in r - assert 'domain' in r - assert len(r['domain']) == 2 # valid country tld + def test_country_table(table): + for r in table.rows: + # "normal" fields exist + assert 'name' in r + # unknown fields are removed/not accessible + assert not 'does-not-exist' in r + # ...so are excluded fields + assert not 'id' in r + # missing data is available with default values + assert 'null' in r + assert r['null'] == "foo" # note: different from prev. line! + + # all that still works when name overrides are used + assert not 'tld' in r + assert 'domain' in r + assert len(r['domain']) == 2 # valid country tld + test_country_table(countries) + + # repeat the avove tests with a table that is not associated with a + # model, and all columns being created manually. + class CountryTable(tables.ModelTable): + name = tables.Column() + population = tables.Column() + capital = tables.Column() + system = tables.Column() + null = tables.Column(default="foo") + tld = tables.Column(name="domain") + countries = CountryTable(Country) + test_country_table(countries) # make sure the row and column caches work for model tables as well assert id(list(countries.columns)[0]) == id(list(countries.columns)[0]) @@ -158,5 +173,4 @@ def test_pagination(): # TODO: pagination # TODO: support function column sources both for modeltables (methods on model) and static tables (functions in dict) # TODO: manual base columns change -> update() call (add as example in docstr here) -> rebuild snapshot: is row cache, column cache etc. reset? -# TODO: throw an exception on invalid order_by -# 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 -- 2.26.2