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

excluded column not removed from sequence #205

Closed
mbertheau opened this issue Aug 6, 2014 · 4 comments · Fixed by #341
Closed

excluded column not removed from sequence #205

mbertheau opened this issue Aug 6, 2014 · 4 comments · Fixed by #341

Comments

@mbertheau
Copy link
Contributor

With this code:

class ParticipantTable(Table):
    email = Column(verbose_name=_("Email"), accessor='user.email')
    register_datetime = Column(verbose_name=_("Register date"))

    class Meta(DashboardTable.Meta):
        model = Participant
        fields = ()
        sequence = ('email', 'register_datetime')


class ParticipantCancelTable(ParticipantTable):
    status = TemplateColumn(verbose_name=_("Status"),
                            template_name='dashboard/catalogue/participant_row_status.html',
                            order_by='status')

    class Meta(ParticipantTable.Meta):
        pass


class TrainerParticipantTable(ParticipantCancelTable):
    class Meta(ParticipantCancelTable.Meta):
        exclude = ('register_datetime', 'order', 'actions')

I get

  File "/home/markus/src/machtfit/apps/dashboard/catalogue/tables.py", line 94, in <module>
    class TrainerParticipantTable(ParticipantCancelTable):
  File "/home/markus/src/django-tables2/django_tables2/tables.py", line 203, in __new__
    attrs["base_columns"] = SortedDict(((x, attrs["base_columns"][x]) for x in opts.sequence))
  File "/home/markus/src/django/django/utils/datastructures.py", line 135, in __init__
    for key, value in data:
  File "/home/markus/src/django-tables2/django_tables2/tables.py", line 203, in <genexpr>
    attrs["base_columns"] = SortedDict(((x, attrs["base_columns"][x]) for x in opts.sequence))
KeyError: u'register_datetime'

The reason is that while ParticipantTable.sequence is enforced by reordering base_columns, a potential exclusion is not taken into account.

A workaround is to specify sequence on TrainerParticipantTable and leave 'register_datetime` out.

@onnokort
Copy link
Contributor

Upvote! Fell into the exactly same trap with django_tables2 version 1.0.4.

jieter added a commit that referenced this issue Jun 4, 2016
@jieter
Copy link
Owner

jieter commented Jun 4, 2016

Finally started looking at this. I reduced the example to this:

    class PersonTable(tables.Table):
        class Meta:
            model = Person
            fields = ()
            sequence = ('first_name', 'last_name', 'occupation')

    class AnotherPersonTable(PersonTable):
        class Meta(PersonTable.Meta):
            exclude = ('first_name', 'last_name')

Resulting in the error in django_tables2/tables.py:222:

>   base_columns = OrderedDict(((x, base_columns[x]) for x in opts.sequence))
E   KeyError: u'first_name'

@jieter
Copy link
Owner

jieter commented Jun 4, 2016

@mbertheau Thanks for reporting the issue, sorry it took so long to fix. Next time it will really help to have a simple, compact reproducible test case.

@jieter
Copy link
Owner

jieter commented Oct 30, 2016

It seems this is not fixed at all, because instantiating the AnotherPersonTable raises a KeyError. This is a more complete test case, which currently fails:

def test_exclude_should_work_on_sequence_too():
    '''
    It should be possible to define a sequence on a table
    and exclude it in a child of that table.
    '''
    class PersonTable(tables.Table):
        first_name = tables.Column()
        last_name = tables.Column()
        occupation = tables.Column()

        class Meta:
            model = Person
            sequence = ('first_name', 'last_name', 'occupation')

    class AnotherPersonTable(PersonTable):
        class Meta(PersonTable.Meta):
            exclude = ('first_name', 'last_name')

    tableA = PersonTable(Person.objects.all())
    assert tableA.columns.names() == ['first_name', 'last_name', 'occupation']

    tableB = AnotherPersonTable(Person.objects.all())
    assert tableB.columns.names() == ['occupation']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants