Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions django_tables2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

from django.core.exceptions import ImproperlyConfigured
from django.views.generic.list import ListView
from django.views.generic.list import (
MultipleObjectMixin as ConfoundingMultipleObjectMixin,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for the alias? Can't we just do from django.views.generic.list import ListView, MultipleObjectMixin?

)

from . import tables
from .config import RequestConfig
Expand Down Expand Up @@ -146,8 +149,19 @@ def get_table_kwargs(self):
return {}

def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
"""Overridden version of `.TemplateResponseMixin` to inject the table into the template's context."""
context = super().get_context_data(**kwargs)
"""
Overridden version of `.TemplateResponseMixin` to inject the table into the template's context.

Will avoid calling ``django.views.generic.list.ListView.get_context_data``
if this mixin is combined with ``django.views.generic.list.ListView`` or
similar, as presumably ListView.get_context_data is meant to fetch the
same data as this function will fetch directly.
"""
context = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are multiple overrides of get_context_data, this could skip more than just the MultipleObjectMixin.get_context_data that this PR tries to skip.

I feel as if the current implementation could have unintended breaking side-effects.

super(ConfoundingMultipleObjectMixin, self).get_context_data(**kwargs)
if isinstance(self, ConfoundingMultipleObjectMixin)
else super().get_context_data(**kwargs)
)
table = self.get_table(**self.get_table_kwargs())
context[self.get_context_table_name(table)] = table
return context
Expand Down