<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Bharadanayan on Medium]]></title>
        <description><![CDATA[Stories by Bharadanayan on Medium]]></description>
        <link>https://medium.com/@bharadanayan47?source=rss-c91a3be70002------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Bharadanayan on Medium</title>
            <link>https://medium.com/@bharadanayan47?source=rss-c91a3be70002------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 14:04:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bharadanayan47/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Why Your Django App is Slow And It’s Probably Your Database Queries ]]></title>
            <link>https://medium.com/@bharadanayan47/why-your-django-app-is-slow-and-its-probably-your-database-queries-3d922bd81c1a?source=rss-c91a3be70002------2</link>
            <guid isPermaLink="false">https://medium.com/p/3d922bd81c1a</guid>
            <dc:creator><![CDATA[Bharadanayan]]></dc:creator>
            <pubDate>Sun, 01 Mar 2026 12:51:13 GMT</pubDate>
            <atom:updated>2026-03-01T12:51:13.741Z</atom:updated>
            <content:encoded><![CDATA[<p>Most Django applications don’t stop working they just crawl. You check your server, your caching, your frontend but the real culprit is often deeper <strong>inefficient database queries</strong>.</p><p>Django’s ORM is powerful and convenient, but if used carelessly, it can generate a lot of unnecessary queries. These add up fast and slow down your entire application.</p><p>Let’s break down what’s really happening and how to fix it.</p><h3>🧠 1. Understanding the Problem</h3><p>When a user requests data, Django translates ORM calls into SQL queries. But even innocent-looking Django code can produce multiple redundant queries behind the scenes.</p><p>A single model call like user.posts.all()might look harmless, but under the hood it can trigger <strong>many database round trips</strong>. Each query adds latency, especially under heavy load.</p><h3>❗ 2. The N+1 Query Problem</h3><p>Imagine you’re displaying a list of blog posts with author names. You fetch all posts, then loop over them to get the author details.</p><pre>posts = Post.objects.all()<br>for post in posts:<br>    print(post.author.name)</pre><p>This seems fine, but Django executes:</p><ul><li>1 query to get all posts</li><li>Then 1 query <em>per post</em> to fetch its author</li></ul><p>So if you have 100 posts? That’s <strong>101 queries</strong>.</p><p>This “N+1 problem” is one of the most common performance killers in Django apps.</p><h3>🚀 3. The Solution: Use select_related and prefetch_related</h3><p>Django provides two powerful tools to <strong>load related objects efficiently</strong>:</p><h3>🔹 select_related</h3><p>Use this when your models are connected with <strong>foreign keys or one-to-one relationships</strong>.</p><pre>posts = Post.objects.select_related(&#39;author&#39;)</pre><p>This makes Django fetch posts <strong>and author data in a single query</strong>, dramatically reducing database hits.</p><h3>🔹 prefetch_related</h3><p>When you have <strong>many-to-many or one-to-many relations</strong>, use this:</p><pre>posts = Post.objects.prefetch_related(&#39;tags&#39;, &#39;comments&#39;)</pre><p>Instead of running queries in a loop, Django will fetch related data in batches.</p><h3>📊 4. Spotting Query Performance Issues</h3><h3>🔍 Use Django Debug Toolbar</h3><p>Install the <strong>Django Debug Toolbar</strong> to see every SQL query your views are making. It shows:</p><ul><li>Total number of queries</li><li>Time taken per query</li><li>SQL text for each query</li></ul><p>This makes it easier to spot places where the number of queries can be reduced.</p><h3>💡 5. Other Common Query Pitfalls</h3><h3>❌ Fetching Too Much Data</h3><p>Avoid retrieving all rows when you only need a few:</p><pre># Bad<br>users = User.objects.all()</pre><pre># Better<br>users = User.objects.only(&#39;id&#39;, &#39;username&#39;)</pre><h3>❌ Using Python to Filter Instead of SQL</h3><p>Filters performed in Python loops instead of at the database level cause extra work and slower responses.</p><h3>🛠️ 6. Cache Where It Makes Sense</h3><p>Even with optimized queries, repeated hits to the database can slow things down.</p><p>Consider caching:</p><ul><li>Whole pages</li><li>Expensive queryset results</li><li>API response fragments</li></ul><p>Django’s caching framework supports Redis, Memcached, and more.</p><h3>📈 7. Measure, Optimize, Repeat</h3><p>Database performance isn’t fixed — it evolves with your app. Every time you:</p><ul><li>Add a new feature</li><li>Change relationships</li><li>Increase traffic</li></ul><p>you should measure performance again using tools like:</p><ul><li><strong>Django Debug Toolbar</strong></li><li><strong>Profiling middleware</strong></li><li><strong>Logging database timing data</strong></li></ul><h3>✨ Final Thoughts</h3><p>Your Django app may not be slow because of Django itself it’s often because the way you’re accessing data causes <em>too many database queries</em>. The ORM makes it easy to write code that looks clean, yet results in inefficient SQL.</p><p>To fix that:<br> ✔️ Understand your query patterns<br> ✔️ Use select_related and prefetch_related<br> ✔️ Avoid N+1 problems<br> ✔️ Limit data retrieved<br> ✔️ Profile regularly</p><p>With these practices, your app will feel <strong>faster, more responsive, and more scalable</strong> without rewriting major parts of your codebase.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3d922bd81c1a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Field name restrictions in Django models]]></title>
            <link>https://medium.com/@bharadanayan47/field-name-restrictions-in-django-models-a3346d4842ee?source=rss-c91a3be70002------2</link>
            <guid isPermaLink="false">https://medium.com/p/a3346d4842ee</guid>
            <category><![CDATA[django-framework]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Bharadanayan]]></dc:creator>
            <pubDate>Sun, 03 Mar 2024 16:11:10 GMT</pubDate>
            <atom:updated>2024-03-03T16:11:10.479Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Introduction</strong></h3><p>Django places some restrictions on model field names It means you can’t keep these as field names in Django model and best practices to flow.</p><figure><img alt="alter" src="https://cdn-images-1.medium.com/max/645/1*liPVHkfRZP3EfdtvhibMRQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/645/1*7IruMud0GTe0-A1n_d8cFA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/645/1*_HbpvAXAzlSLj0F4fv2OQw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/645/1*5puQZP7zfCtokpoT57qT4Q.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/645/1*pYIRvAmcbOVZf0x4-KOefg.jpeg" /></figure><h3><strong>Conclusion</strong></h3><p>In Django modal creation it is essential to avoid common mistakes and provide meaningful names for your fields</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a3346d4842ee" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>