<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Debug &amp;mdash; sandeepk</title>
    <link>https://blogs.dgplug.org/sandeepk/tag:Debug</link>
    <description></description>
    <pubDate>Sun, 19 Apr 2026 07:40:12 +0000</pubDate>
    <item>
      <title>The Debug Diary - Chapter I</title>
      <link>https://blogs.dgplug.org/sandeepk/the-debug-diary-chapter-i</link>
      <description>&lt;![CDATA[The Debug Diary - Chapter I&#xA;&#xA;Lately, I was debugging an issue with the importer tasks of our codebase and came across a code block which looks fine but makes an extra database query in the loop. When you have a look at the Django ORM query&#xA;&#xA;jatovehicles = JatoVehicle.objects.filter(&#xA;    yearin=availableyears,morefilters&#xA;).only(&#34;manufacturercode&#34;, &#34;uid&#34;, &#34;year&#34;, &#34;model&#34;, &#34;trim&#34;)&#xA;&#xA;for entry in jatovehicles.iterator():&#xA;    if entry.manufacturercode:&#xA;        logic&#xA;    ymtkey = (entry.year, entry.model, entry.trimprocessed)&#xA;...&#xA;you will notice we are using only, which only loads the set of fields mentioned and deferred other fields, but in the loop, we are using the field trimprocessed which is a deferred field and will result in an extra database call.&#xA;&#xA;Now, as we have identified the performance issue, the best way to handle the cases like this is to use values or valueslist. The use of only should be discouraged in the cases like these.&#xA;&#xA;Update code will look like this&#xA;jatovehicles = JatoVehicle.objects.filter(&#xA;    yearin=availableyears,more-filters).valueslist(&#xA;    &#34;manufacturercode&#34;,&#xA;    &#34;uid&#34;,&#xA;    &#34;year&#34;,&#xA;    &#34;model&#34;,&#xA;    &#34;trimprocessed&#34;,&#xA;    named=True,&#xA;)&#xA;&#xA;for entry in jatovehicles.iterator():&#xA;    if entry.manufacturercode:&#xA;        logic&#xA;    ymtkey = (entry.year, entry.model, entry.trimprocessed)&#xA;...&#xA;&#xA;By doing this, we are safe from accessing the fields which are not mentioned in the valueslist. If anyone tries to do so, an exception will be raised.&#xA;&#xA;** By using named=True we get the result as a named tuple which makes it easy to access the values :)&#xA;&#xA; &#xA;Cheers!&#xA;&#xA;Django&#xA;ORM&#xA;Debug]]&gt;</description>
      <content:encoded><![CDATA[<h2 id="the-debug-diary-chapter-i">The Debug Diary – Chapter I</h2>

<p>Lately, I was debugging an issue with the importer tasks of our codebase and came across a code block which looks fine but makes an extra database query in the loop. When you have a look at the Django ORM query</p>

<pre><code class="language-python">jato_vehicles = JatoVehicle.objects.filter(
    year__in=available_years,&lt;more_filters&gt;
).only(&#34;manufacturer_code&#34;, &#34;uid&#34;, &#34;year&#34;, &#34;model&#34;, &#34;trim&#34;)

for entry in jato_vehicles.iterator():
    if entry.manufacturer_code:
        &lt;logic&gt;
    ymt_key = (entry.year, entry.model, entry.trim_processed)
...
</code></pre>

<p>you will notice we are using <code>only</code>, which only loads the set of fields mentioned and deferred other fields, but in the loop, we are using the field <code>trim_processed</code> which is a deferred field and will result in an extra database call.</p>

<p>Now, as we have identified the performance issue, the best way to handle the cases like this is to use <code>values</code> or <code>values_list</code>. The use of <code>only</code> should be discouraged in the cases like these.</p>

<p>Update code will look like this</p>

<pre><code class="language-python">jato_vehicles = JatoVehicle.objects.filter(
    year__in=available_years,&lt;more-filters&gt;).values_list(
    &#34;manufacturer_code&#34;,
    &#34;uid&#34;,
    &#34;year&#34;,
    &#34;model&#34;,
    &#34;trim_processed&#34;,
    named=True,
)

for entry in jato_vehicles.iterator():
    if entry.manufacturer_code:
        &lt;logic&gt;
    ymt_key = (entry.year, entry.model, entry.trim_processed)
...
</code></pre>

<p>By doing this, we are safe from accessing the fields which are not mentioned in the <code>values_list</code>. If anyone tries to do so, an exception will be raised.</p>

<p>** By using <code>named=True</code> we get the result as a named tuple which makes it easy to access the values :)</p>

<p>Cheers!</p>

<p><a href="/sandeepk/tag:Django" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Django</span></a>
<a href="/sandeepk/tag:ORM" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">ORM</span></a>
<a href="/sandeepk/tag:Debug" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Debug</span></a></p>
]]></content:encoded>
      <guid>https://blogs.dgplug.org/sandeepk/the-debug-diary-chapter-i</guid>
      <pubDate>Tue, 30 Aug 2022 07:34:12 +0000</pubDate>
    </item>
  </channel>
</rss>