Queryset repr trick

Posted on czw 15 lutego 2018 in misc

Sometimes, when I'm debugging something using pudb (probably it affects any interactive debugger), pudb is reflecting value of variables of QuerySet type, which makes unneccesary query to database. It makes debugging slow, and sometimes it breaks debugging context because of making changes in db.

To avoid that, add following code in bottom of your settings.py in django app:

if DEBUG:
    from django.db.models.query import QuerySet
    QuerySet.__repr__ = lambda self: "<QS '%s'>"%str(self.query)