<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Readul Hasan Chayan [Heemayl] (Posts about decorator)</title><link>https://heemayl.net/</link><description></description><atom:link href="https://heemayl.net/categories/decorator.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><copyright>Contents © 2024 &lt;a href="mailto:me@heemayl.net"&gt;Readul Hasan Chayan [Heemayl]&lt;/a&gt; 
&lt;a rel="license" href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank"&gt;
&lt;img alt="Creative Commons License BY-NC-SA"
style="border-width:0; margin-top: 10px; margin-bottom: 5px;"
src="https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png"&gt;&lt;/a&gt;
</copyright><lastBuildDate>Thu, 07 Mar 2024 19:03:10 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Patching decorators in Python</title><link>https://heemayl.net/posts/patching-decorators-in-python/</link><dc:creator>Readul Hasan Chayan [Heemayl]</dc:creator><description>&lt;div&gt;&lt;p&gt;Being a &lt;em&gt;syntactic sugar&lt;/em&gt; decorators are essentially a usually callable (e.g. a function or a class) that wraps another callable. And essentially they are applied/called when the module is being read.&lt;/p&gt;
&lt;p&gt;For example, the following two chunks of codes are identical:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span class="nd"&gt;@decorator&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;function&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;
&lt;/pre&gt;
&lt;pre class="code literal-block"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;function&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;

&lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;decorator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;As we can see the &lt;code&gt;decorator&lt;/code&gt; in this example is called when the file/module containing the above would be read. So how do we patch such an object?&lt;/p&gt;
&lt;p&gt;&lt;a href="https://heemayl.net/posts/patching-decorators-in-python/"&gt;Read more…&lt;/a&gt; (1 min remaining to read)&lt;/p&gt;&lt;/div&gt;</description><category>decorator</category><category>patch</category><category>python</category><guid>https://heemayl.net/posts/patching-decorators-in-python/</guid><pubDate>Sat, 25 Jun 2022 13:28:34 GMT</pubDate></item><item><title>Django: Clone a model instance on update</title><link>https://heemayl.net/posts/django-clone-a-model-instance-on-update/</link><dc:creator>Readul Hasan Chayan [Heemayl]</dc:creator><description>&lt;div&gt;&lt;p&gt;Often times, we need to clone a Django model instance while updating it's fields' values. For example, if we have a price model that needs updating, it needs to be cloned first to keep the current related objects referring to the current pricing values. So while updating, we need to keep the current instance as-is and create a new instance with existing data and update the new instance with updated data instead.&lt;/p&gt;
&lt;p&gt;If we don't do the above, when we change any relevant field's value, the related objects will subject to the new values, this will in turn trigger new pricing which would be different from the originally calculated/saved one. This might cause serious reliability problems like the initial price/charge was 20 USD, now it has become 30 USD (for example), and so on.&lt;/p&gt;
&lt;p&gt;So to handle this automatic cloning of instances on update, I've written a decorator that can be used as a decorator on the &lt;code&gt;save&lt;/code&gt; method of the model. Here it is (all the necesary details are in the docstring):&lt;/p&gt;
&lt;p&gt;&lt;a href="https://heemayl.net/posts/django-clone-a-model-instance-on-update/"&gt;Read more…&lt;/a&gt; (4 min remaining to read)&lt;/p&gt;&lt;/div&gt;</description><category>decorator</category><category>descriptor</category><category>django</category><category>django_model</category><category>python</category><guid>https://heemayl.net/posts/django-clone-a-model-instance-on-update/</guid><pubDate>Thu, 24 Oct 2019 15:26:37 GMT</pubDate></item></channel></rss>