AsheKR
django-query-capture
Python

Shows queries, find slow queries, detects N+1 in Django

Last updated Jul 8, 2025
92
Stars
7
Forks
13
Issues
0
Stars/day
Attention Score
4
Language breakdown
No language data available.
โ–ธ Files click to expand
README

django-query-capture

Build status Python Version Dependencies Status

Code style: black Security: bandit Pre-commit Semantic Versions License Coverage Report

Overview

img.png

Django Query Capture can check the query situation at a glance, notice slow queries, and notice where N+1 occurs.

Some reasons you might want to use django-query-capture:

  • It can be used to simply check queries in a specific block.
  • It supports Django Middleware, Context Manager, and Decorator.
  • When you use Context Manager, you can get real-time query data.
  • You can see where the query occurs.
  • Inefficient queries can be found in the test code.
  • It is easy to customize by simply changing the table shape, changing the color, and selecting and setting the desired output.
  • It supports customization that allows you to decorate the output freely from the beginning.
  • Fully Documented
  • It supports Type hint everywhere.

Simple Usage

  • Just add it to Middleware without any other settings, and it will be output whenever a query occurs.
MIDDLEWARE = [
  ...,
  "djangoquerycapture.middleware.QueryCaptureMiddleware",
]
  • Use in function-based views. or just function
from djangoquerycapture import query_capture

@query_capture() def my_view(request): pass

  • Use in class-based views.
from django.utils.decorators import method_decorator
from django.views.generic import TemplateView
from djangoquerycapture import query_capture

@methoddecorator(querycapture, name='dispatch') class AboutView(TemplateView): pass

  • Use it as a context.
When used as Context, you can check the query in real time.
from djangoquerycapture import query_capture

from tests.news.models import Reporter

@query_capture() def run_something(): with query_capture() as capture: Reporter.objects.create(full_name=f"target-1") print(len(capture.captured_queries)) # console: 1 Reporter.objects.create(full_name=f"target-2") print(len(capture.captured_queries)) # console: 2

  • Use in test
Test code can capture inefficient queries through the AssertInefficientQuery Util.
from django.test import TestCase

from djangoquerycapture.test_utils import AssertInefficientQuery

class AssertInefficientQueryTests(TestCase): def testassertinefficient_query(self): with AssertInefficientQuery(num=19): self.client.get('/api/reporter') # desire threshold count 19 but, /api/reporter duplicate query: 20, so raise error

Installation

pip install -U django-query-capture

or install with Poetry

poetry add django-query-capture

Full Documentation

Extension documentation is found here: https://ashekr.github.io/django-query-capture/.

๐Ÿ›ก License

License

This project is licensed under the terms of the MIT license. See LICENSE for more details.

Credits ๐Ÿš€ Your next Python package needs a bleeding-edge project structure.

This project was generated with python-package-template

ยฉ 2026 GitRepoTrend ยท AsheKR/django-query-capture ยท Updated daily from GitHub