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

TableQuerysetData.__len__ calls len() on a QuerySet instead of using count() #833

Open
ercpe opened this issue Mar 3, 2022 · 1 comment

Comments

@ercpe
Copy link

ercpe commented Mar 3, 2022

Hi,

the __len__ implementation of TableQuerysetData class only checks for the presence of the paginator to determine to use .count() over len(): https://github.com/jieter/django-tables2/blob/master/django_tables2/data.py#L150
Unfortunately, __len__ of a QuerySet is implemented to fetch all records and using len() on the result list: https://github.com/django/django/blob/main/django/db/models/query.py#L302

This can be a really expensive operation. I suggest to change the condition to

if hasattr(self.table, "paginator") or isinstance(self.data, QuerySet):

to utilize the much faster .count() query in case of a QuerySet.

@tuky
Copy link
Contributor

tuky commented Apr 8, 2022

what would be the benefit? without pagination, the whole queryset will be listed anyway, right? so the current code version is already optimized and saves the count() call.

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

No branches or pull requests

2 participants