Skip to main content

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 = [
  ...,
  "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(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

Metadata

Release files for django-query-capture 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-query-capture 0.3.2
File Size Uploaded
django-query-capture-0.3.2.tar.gz 14.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-query-capture 0.3.2
File Interpreter ABI Platform
django_query_capture-0.3.2-py3-none-any.whl Python 3 none any Details

Total release size: 30.4 kB

Release files / django-query-capture-0.3.2.tar.gz

Download URL django-query-capture-0.3.2.tar.gz
Size 14.1 kB
Tags Source
SHA-256 checksum
How to use checksums
8c7e2b842df7166fcfbe290b4061f2784871f74d888820607683465b8cc06299
BLAKE2b-256 checksum
How to use checksums
c55ed90187db64df0e7db92f55c574c5cf6c0c28640acd08351c786d562fc4f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.10.2 Linux/5.13.0-1031-azure

Release files / django_query_capture-0.3.2-py3-none-any.whl

Download URL django_query_capture-0.3.2-py3-none-any.whl
Size 16.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9979829e120cb2f66d651765fcb2b870cf08184c882322ed5e8478ff3929e97a
BLAKE2b-256 checksum
How to use checksums
86a7c3a753fa4b456d88900ba2d5335fcf20e009260e312fcf4497b47c9d7839
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.10.2 Linux/5.13.0-1031-azure

Release history Release notifications | RSS feed

This release

0.3.2 This release

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.9

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page