<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: atiqur rahman</title>
    <description>The latest articles on DEV Community by atiqur rahman (@atiqur_rahman_5998740c55f).</description>
    <link>https://dev.to/atiqur_rahman_5998740c55f</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4098340%2F3f77910e-bf51-4f74-949e-383ae7c2cd76.png</url>
      <title>DEV Community: atiqur rahman</title>
      <link>https://dev.to/atiqur_rahman_5998740c55f</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/atiqur_rahman_5998740c55f"/>
    <language>en</language>
    <item>
      <title>How to Stop N+1 Database Queries in Django Tests with django-query-guard</title>
      <dc:creator>atiqur rahman</dc:creator>
      <pubDate>Fri, 28 Aug 2026 05:07:11 +0000</pubDate>
      <link>https://dev.to/atiqur_rahman_5998740c55f/how-to-stop-n1-database-queries-in-django-tests-with-django-query-guard-dk2</link>
      <guid>https://dev.to/atiqur_rahman_5998740c55f/how-to-stop-n1-database-queries-in-django-tests-with-django-query-guard-dk2</guid>
      <description>&lt;p&gt;N+1 database queries and silent query count growth can severely affect Django application performance. Unfortunately, they are often discovered too late—in production server logs or APM dashboards.&lt;/p&gt;

&lt;p&gt;To solve this problem, I built &lt;strong&gt;&lt;code&gt;django-query-guard&lt;/code&gt;&lt;/strong&gt;—an open-source Pytest plugin that brings database performance checks directly into automated test suites.&lt;/p&gt;




&lt;h2&gt;
  
  
  💡 The Problem: Silent N+1 Queries
&lt;/h2&gt;

&lt;p&gt;In Django, the ORM makes fetching related models effortless, but it’s terrifyingly easy to write an accidental N+1 loop:&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
python
# ❌ N+1 Query Accident
# Fetches 100 users (1 query), then executes 100 individual queries for each profile!
# Total: 101 Database Queries! 🐌
users = User.objects.all()
profiles = [user.profile.bio for user in users]

🛡️ The Solution: django-query-guard
Instead of relying on manual code reviews, django-query-guard turns query limits and N+1 prevention into enforceable Pytest assertions:

python


import pytest
@pytest.mark.django_db
@pytest.mark.query_guard(max_queries=2, detect_n_plus_one=True)
def test_user_profiles_api(client):
    response = client.get("/api/users/")
    assert response.status_code == 200
If your endpoint accidentally executes 101 queries instead of 2, Pytest fails instantly with an exact breakdown of which SQL statement repeated! 💥

🔥 Key Features
🎯 Pytest Marker Integration: @pytest.mark.query_guard(max_queries=N)
🧠 Smart SQL Normalization: Parameter-variation filtering prevents false positives on intentional duplicate queries with identical parameters.
🗄️ Multi-Database Support: Monitors queries across all configured Django database connections (default, replica, etc.).
📊 HTML Query Reports: Dark-themed HTML report generation after test runs (pytest --query-guard-report=report.html).
📈 Trend History Tracking: JSON-based run history with regression detection across test builds (pytest --query-guard-trend=.query_guard_history.json).
🐍 Wide Compatibility: Fully tested on Python 3.10–3.14 and Django 4.0–6.0.
📦 Installation &amp;amp; Setup
Install the stable package from PyPI:

bash


pip install django-query-guard
Or with development tools:

bash


pip install django-query-guard[dev]
📊 Generating Visual HTML Reports
You can generate a dark-themed HTML query report after any test run:

bash


pytest --query-guard-report=report.html
The report includes:

Summary metrics (Total tests, pass/fail counts, total queries, N+1 detections)
Per-test query count breakdown
Detailed SQL query execution duration and parameters for failing tests

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>django</category>
      <category>python</category>
      <category>pytest</category>
      <category>playwright</category>
    </item>
  </channel>
</rss>
