Skip to content

Commit

Permalink
mibios/query: stop __repr__() from evaluating the queryset
Browse files Browse the repository at this point in the history
  * adds peek() queryset method to do what Django's __repr__ does.

To many times expensive DB queries get triggered just because we're
running print somewhere.
  • Loading branch information
robert102 committed Nov 21, 2024
1 parent 224a8c6 commit d9dbc4e
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions mibios/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,18 @@ def __init__(self, *args, manager=None, **kwargs):
self._rev_rel_count_fields = []
self._manager = manager

def peek(self):
""" The original __repr__() method of Django querysets """
return super().__repr__()

def __repr__(self):
""" Replacement that doesn't hit the DB """
if self._result_cache is None:
info = '(not evaluated)'
else:
info = f'length={len(self)}'
return f'{type(self).__name__}/{self.model.__name__}:{info}'

@classmethod
def pd_type(cls, field):
"""
Expand Down

0 comments on commit d9dbc4e

Please sign in to comment.