iommirocks / iommi

Your first pick for a django power cord

Home Page:http://iommi.rocks

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Automatic index filtering is not working if columns are named in the Table class

dolikemc opened this issue · comments

Difficult to describe and maybe a no-issue or by design.

The query_from_indexes is creating a query section only if the filter columns are not named columns in the Table class

     class DataTable(Table): # working example
        class Meta:
              auto__model = DataModel
              auto__include = ['name', 'description']
              query_from_indexes = True


    class ColumnDataTable(DataTable): # not working 
        name = Column()
        description = Column()

Also add a small test case with some instructions, hopefully not too confusing:

    """
    file name: test/test_iommi_table_filter.py
    
    additional files in test:
    
    test/apps.py
        from django.apps import AppConfig
    
    
        class TestConfig(AppConfig):
            default_auto_field = 'django.db.models.BigAutoField'
            name = 'test'
    
    test/models.py
    
        from django.db import models
    
    
        class DataModel(models.Model):
            name = models.CharField(max_length=32, unique=True, db_index=True)
            description = models.TextField(null=True, blank=True)
    
    
    setting.py
    
        if int(os.getenv('TESTING', 0)) == 1:
            print('testing.........')
            INSTALLED_APPS.append('test')
    
    urls.py
    
        if os.getenv('TESTING', 0) == '1':
            urlpatterns += [path('test_a/', DataTable().as_view(),
                name='test-a'),
            path('test_b/', ColumnDataTable().as_view(),
                name='test-b')]
    """
    
    from django.test import TestCase, Client
    from django.urls import reverse_lazy
    
    from iommi import Table, Column
    
    from test.models import DataModel  # see above
    
    
    class DataTable(Table):
        class Meta:
            auto__model = DataModel
            auto__include = ['name', 'description']
            query_from_indexes = True
    
    
    class ColumnDataTable(DataTable):
        name = Column()
        description = Column()
    
    
    class IommiTableTests(TestCase):
        def setUp(self) -> None:
            self.client = Client()
    
        def test_filter_wo_columns(self) -> None:
            url = reverse_lazy('test-a')  # see urls.py adjustment
            repsonse = self.client.get(url)
            self.assertEqual(repsonse.status_code, 200)
            self.assertIn('<div id="iommi_query">', str(repsonse.content))
    
        def test_filter_with_columns(self) -> None:
            url = reverse_lazy('test-b')  # see urls.py adjustment
            repsonse = self.client.get(url)
            self.assertEqual(repsonse.status_code, 200)
            self.assertIn('<div id="iommi_query">', str(repsonse.content))

The second test case fails.

if you change it to

    class ColumnDataTable(DataTable):
            name = Column(filter__include=True)
            description = Column()

the second test case passes. But in more complex scenarios you will then might loose all the nice automation from the query_from_index switch (at least in my experience).

I do think this is by design yea. It's so that you can later totally override a configuration of column.

In this specific example, if you just remove the name and description column definitions from the subclass it will work as you want I think.