Shows queries, detects N+1 in Django, Simple to use, Can Customize Console Result
Project description
django-query-capture
Overview
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 = [
...,
"django_query_capture.middleware.QueryCaptureMiddleware",
]
- Use in function-based views. or just function
from django_query_capture 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 django_query_capture import query_capture
@method_decorator(query_capture, name='dispatch')
class AboutView(TemplateView):
pass
- Use it as a context.
When used as Context, you can check the query in real time.
from django_query_capture 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 django_query_capture.test_utils import AssertInefficientQuery
class AssertInefficientQueryTests(TestCase):
def test_assert_inefficient_query(self):
with AssertInefficientQuery(self, 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
🛡 License
This project is licensed under the terms of the MIT
license. See LICENSE for more details.
Credits
This project was generated with python-package-template
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
django-query-capture-0.2.0.tar.gz
(14.0 kB
view hashes)
Built Distribution
Close
Hashes for django-query-capture-0.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d5b33b54a36299a7c4a9f21c044fe429ae5a7892151e2ecfec6eb5fe9ac88a7 |
|
MD5 | dfdf75af56f42d44a19103e988c9aebe |
|
BLAKE2b-256 | bae5b0f8c44a1c29053a785d2a8e0331987b246ef309571092a5a6c8924420c4 |
Close
Hashes for django_query_capture-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9513e99e724eda00fff62f909c9ee9fabd821bdbf44a8f42b66939aa4018d361 |
|
MD5 | 739c819a44c90203aaa83d8162ea0c5b |
|
BLAKE2b-256 | 581639e6087b3af74f9dd334f380f7c85fcfc21a7a93bed5090bb59cd3e08102 |