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

Avoid fetching table data in duplicate from ListView mixin #986

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
16 changes: 14 additions & 2 deletions django_tables2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
from typing import Any, Optional

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

from . import tables
from .config import RequestConfig
Expand Down Expand Up @@ -156,8 +159,17 @@ def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
"""
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 = super().get_context_data(**kwargs)
context = (
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
Loading