django-query-doctor — Automated diagnosis & prescriptions for slow Django ORM queries

Hi everyone! :waving_hand:

I’m Hassan, a Senior Backend Engineer working primarily with Django. I just open-sourced django-query-doctor — a drop-in package that automatically detects and fixes common Django ORM performance problems.

The problem it solves:

We’ve all been bitten by N+1 queries, missing indexes, or bloated SELECT * calls that silently slow down our apps. Finding them usually means digging through django-debug-toolbar panels or manually adding select_related everywhere. I wanted something that catches these issues automatically, tells you exactly where they are in your code, and gives you a copy-paste fix.

How it works:

Install it, add the middleware, and it intercepts your queries at runtime through a four-stage pipeline: Intercept → Fingerprint → Analyze → Report. No DEBUG=True required — it hooks into connection.execute_wrapper().

bash

pip install django-query-doctor

python

# settings.py
INSTALLED_APPS = [
    ...
    "query_doctor",
]

MIDDLEWARE = [
    ...
    "query_doctor.middleware.QueryDoctorMiddleware",
]

That’s it. Zero required configuration.

What it detects (7 built-in analyzers):

  • N+1 queries

  • Duplicate queries

  • Missing index opportunities

  • Fat SELECT (selecting columns you never use)

  • Unintended QuerySet evaluation

  • DRF serializer inefficiencies

  • Query complexity issues

What it gives you:

Every issue comes back as a Prescription with severity, description, exact file and line number, and a ready-to-apply code fix.

Reporting options: Rich console output, JSON, HTML dashboard, log file, and OpenTelemetry export.

Management commands:

  • check_queries — Run analysis across your project

  • query_budget — Set query count budgets per endpoint

  • fix_queries — Auto-apply suggested fixes

  • diagnose_project — Full project health check

Also includes: pytest plugin, Celery task support, async/ASGI support, CI diff-aware mode, and an admin dashboard.

Some numbers: 434 tests, 86%+ coverage, supports Python 3.10+ and Django 4.2+.

I’d really appreciate any feedback, feature suggestions, or bug reports. If you find it useful, a GitHub star would mean a lot — I’m working toward getting it listed in awesome-django.

Thanks for reading!

2 Likes