<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>ORM &amp;mdash; sandeepk</title>
    <link>https://blogs.dgplug.org/sandeepk/tag:ORM</link>
    <description></description>
    <pubDate>Sun, 19 Apr 2026 06:13:07 +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>
    <item>
      <title>Django: How to acquire a lock on the database rows? </title>
      <link>https://blogs.dgplug.org/sandeepk/django-how-to-acquire-a-lock-on-the-database-rows</link>
      <description>&lt;![CDATA[selectforupdate is the answer if you want to acquire a lock on the row. The lock is only released after the transaction is completed. This is similar to the Select for update statement in the SQL query.&#xA;&#xA;      Dealership.objects.selectforupdate().get(pk=&#39;iamid&#39;)&#xA;      # Here lock is only required on Dealership object&#xA;      Dealership.objects.selectrelated(&#39;oem&#39;).selectforupdate(of=(&#39;self&#39;,))&#xA;selectforupdate have these four arguments with these default value&#xA;nowait=False&#xA;skiplocked=False&#xA;of=()&#xA;nokey=False&#xA;&#xA;Let&#39;s see what these all arguments mean&#xA;&#xA;nowait&#xA;Think of the scenario where the lock is already acquired by another query, in this case, you want your query to wait or raise an error, This behavior can be controlled by nowait, If nowait=True we will raise the DatabaseError otherwise it will wait for the lock to be released.&#xA;skiplocked&#xA;As somewhat name implies, it helps to decide whether to consider a locked row in the evaluated query. If the skiplocked=true locked rows will not be considered.&#xA;&#xA;  nowait and skiplocked are mutually exclusive using both together will raise ValueError&#xA;&#xA;of&#xA;In selectforupdate when the query is evaluated, the lock is also acquired on the select related rows as in the query. If one doesn&#39;t wish the same, they can use of where they can specify fields to acquire a lock on&#xA;      Dealership.objects.selectrelated(&#39;oem&#39;).selectforupdate(of=(&#39;self&#39;,))&#xA;Just be sure we don&#39;t have any nullable relation with OEM&#xA;nokey&#xA;This helps you to create a weak lock. This means the other query can create new rows which refer to the locked rows (any reference relationship).&#xA;&#xA;Few more important points to keep in mind selectforupdate doesn&#39;t allow nullable relations, you have to explicitly exclude these nullable conditions. In auto-commit mode, selectforupdate fails with error TransactionManagementError you have to add code in a transaction explicitly. I have struggled around these points :).&#xA;&#xA;Here is all about selectforupdate which you require to know to use in your code and to do changes to your database.&#xA;&#xA;Cheers!&#xA;&#xA;Python&#xA;Django&#xA;ORM&#xA;Database&#xA;]]&gt;</description>
      <content:encoded><![CDATA[<p><code>select_for_update</code> is the answer if you want to acquire a lock on the row. The lock is only released after the transaction is completed. This is similar to the <code>Select for update statement</code> in the SQL query.</p>

<pre><code class="language-python">&gt;&gt;&gt; Dealership.objects.select_for_update().get(pk=&#39;iamid&#39;)
&gt;&gt;&gt; # Here lock is only required on Dealership object
&gt;&gt;&gt; Dealership.objects.select_related(&#39;oem&#39;).select_for_update(of=(&#39;self&#39;,))
</code></pre>

<p><code>select_for_update</code> have these four arguments with these default value
– nowait=False
– skip<em>locked=False
– of=()
– no</em>key=False</p>

<p>Let&#39;s see what these all arguments mean</p>

<h2 id="nowait">nowait</h2>

<p>Think of the scenario where the lock is already acquired by another query, in this case, you want your query to wait or raise an error, This behavior can be controlled by <code>nowait</code>, If <code>nowait=True</code> we will raise the <code>DatabaseError</code> otherwise it will wait for the lock to be released.</p>

<h2 id="skip-locked">skip_locked</h2>

<p>As somewhat name implies, it helps to decide whether to consider a locked row in the evaluated query. If the <code>skip_locked=true</code> locked rows will not be considered.</p>

<blockquote><p>nowait and skip_locked are mutually exclusive using both together will raise ValueError</p></blockquote>

<h2 id="of">of</h2>

<p>In <code>select_for_update</code> when the query is evaluated, the lock is also acquired on the select related rows as in the query. If one doesn&#39;t wish the same, they can use <code>of</code> where they can specify fields to acquire a lock on</p>

<pre><code class="language-python">&gt;&gt;&gt; Dealership.objects.select_related(&#39;oem&#39;).select_for_update(of=(&#39;self&#39;,))
# Just be sure we don&#39;t have any nullable relation with OEM
</code></pre>

<h2 id="no-key">no_key</h2>

<p>This helps you to create a weak lock. This means the other query can create new rows which refer to the locked rows (any reference relationship).</p>

<p>Few more important points to keep in mind <code>select_for_update</code> doesn&#39;t allow nullable relations, you have to explicitly exclude these nullable conditions. In auto-commit mode, <code>select_for_update</code> fails with error <code>TransactionManagementError</code> you have to add code in a transaction explicitly. I have struggled around these points :).</p>

<p>Here is all about <code>select_for_update</code> which you require to know to use in your code and to do changes to your database.</p>

<p>Cheers!</p>

<p><a href="/sandeepk/tag:Python" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Python</span></a>
<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:Database" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Database</span></a></p>
]]></content:encoded>
      <guid>https://blogs.dgplug.org/sandeepk/django-how-to-acquire-a-lock-on-the-database-rows</guid>
      <pubDate>Sat, 14 May 2022 14:06:03 +0000</pubDate>
    </item>
  </channel>
</rss>