<?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: Rahul Mishra</title>
    <description>The latest articles on DEV Community by Rahul Mishra (@rahul91).</description>
    <link>https://dev.to/rahul91</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F622782%2Fb07982c9-2bcb-4611-a7cf-58088637dc71.png</url>
      <title>DEV Community: Rahul Mishra</title>
      <link>https://dev.to/rahul91</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rahul91"/>
    <language>en</language>
    <item>
      <title>What is Django ContentType</title>
      <dc:creator>Rahul Mishra</dc:creator>
      <pubDate>Mon, 23 Aug 2021 05:31:34 +0000</pubDate>
      <link>https://dev.to/rahul91/what-is-django-contenttype-21i3</link>
      <guid>https://dev.to/rahul91/what-is-django-contenttype-21i3</guid>
      <description>&lt;p&gt;In this series, we will understand Django's ContentType and what is GenericForeignKey with examples. &lt;code&gt;contenttypes&lt;/code&gt; is a Django app that can track all of the models installed in our Django-powered project, providing a high-level, generic interface for working with our models.&lt;/p&gt;

&lt;p&gt;If we check in settings &lt;code&gt;INSTALLED_APPS&lt;/code&gt; we can see, that &lt;code&gt;django.contrib.contenttypes&lt;/code&gt; is readily present.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSTALLED_APPS = [
    ...
    'django.contrib.contenttypes',
    ...
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since &lt;code&gt;contenttypes&lt;/code&gt; is just like any other app, it also has migrations and, and views.py. The most important model of the contenttypes app is &lt;code&gt;ContentType&lt;/code&gt;. Let's see what fields ContentType has.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ContentType(models.Model):
    app_label = models.CharField(max_length=100)
    model = models.CharField(_('python model class name'), max_length=100)
    objects = ContentTypeManager()

    class Meta:
        verbose_name = _('content type')
        verbose_name_plural = _('content types')
        db_table = 'django_content_type'
        unique_together = [['app_label', 'model']]

    def __str__(self):
        return self.app_labeled_name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Apart from the custom manager, it has &lt;code&gt;app_label&lt;/code&gt; and &lt;code&gt;model&lt;/code&gt; both as string fields, which also are &lt;code&gt;unique_together&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;app_label is the name of the application the model is part of.&lt;/li&gt;
&lt;li&gt;model is the name of the model itself.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We have already established that &lt;code&gt;ContentType&lt;/code&gt; is a model that tracks all the models in your Django app and therefore, as soon as we create a new table in the database, a new entry gets created for the new table in the ContentType table. Consequently, &lt;code&gt;ContentType&lt;/code&gt; model objects "points" to a particular table in your Django app. Let's see few entries in the ContentType table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; from django.contrib.contenttypes.models import ContentType

&amp;gt;&amp;gt;&amp;gt; ContentType.objects.filter(model='user').values()
&amp;lt;QuerySet [{'id': 4, 'app_label': 'auth', 'model': 'user'}]&amp;gt;

&amp;gt;&amp;gt;&amp;gt; ContentType.objects.filter(model='contenttype').values()
&amp;lt;QuerySet [{'id': 5, 'app_label': 'contenttypes', 'model': 'contenttype'}]&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since &lt;code&gt;ContentType&lt;/code&gt; itself is a model, the ContentType model also tracks itself and thus has an entry for itself, this sounds a bit confusing but makes complete sense.&lt;/p&gt;

&lt;p&gt;ContentType model has 2 important methods, &lt;code&gt;model_class&lt;/code&gt; and &lt;code&gt;get_object_for_this_type&lt;/code&gt;, let's see with an example what these methods do.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; from django.contrib.contenttypes.models import ContentType

&amp;gt;&amp;gt;&amp;gt; user_ct = ContentType.objects.get(model='user')
&amp;gt;&amp;gt;&amp;gt; user_ct.model_class()
django.contrib.auth.models.User

&amp;gt;&amp;gt;&amp;gt; user_ct.get_object_for_this_type(username='bob')
&amp;lt;User: bob&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Okay, so now at this point we know what ContentType is, but what good is it for? Why do we need this?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GenericForeignKeys are implemented via ContentType, we will see this in the next part of this article about how ContentTypes are used.&lt;/li&gt;
&lt;li&gt;The admin application uses it to log the history of each object added or changed through the admin interface.
Django’s authentication framework uses it to tie user permissions to specific models.&lt;/li&gt;
&lt;li&gt;Since we have all models listed here in one place, we can use this to build generic solutions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article, we understood about &lt;code&gt;ContentType&lt;/code&gt; model and &lt;code&gt;contenttypes&lt;/code&gt; framework, in the next article, we will see one of the implementations of &lt;code&gt;ContentType&lt;/code&gt; which is &lt;code&gt;GenericForeignKey&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To read more, please visit: &lt;a href="https://buildatscale.tech/what-is-django-contenttype/"&gt;https://buildatscale.tech/what-is-django-contenttype/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>django</category>
      <category>python</category>
    </item>
    <item>
      <title>Transaction Isolation Levels In Innodb</title>
      <dc:creator>Rahul Mishra</dc:creator>
      <pubDate>Sun, 11 Jul 2021 15:26:25 +0000</pubDate>
      <link>https://dev.to/rahul91/transaction-isolation-levels-in-innodb-okb</link>
      <guid>https://dev.to/rahul91/transaction-isolation-levels-in-innodb-okb</guid>
      <description>&lt;p&gt;First, let's break the heading, a &lt;strong&gt;Transaction&lt;/strong&gt; is a sequential group of statements, queries, or operations that performs as a single work unit that can be committed or rolled back. Next, &lt;strong&gt;Isolation&lt;/strong&gt; is the &lt;code&gt;I&lt;/code&gt; of &lt;a href="https://en.wikipedia.org/wiki/ACID" rel="noopener noreferrer"&gt;ACID&lt;/a&gt; properties, isolation refers to the ability to concurrently process multiple transactions in a way that one does not affect another. Lastly, &lt;strong&gt;&lt;a href="https://en.wikipedia.org/wiki/InnoDB" rel="noopener noreferrer"&gt;Innodb&lt;/a&gt;&lt;/strong&gt; is the default &lt;a href="https://en.wikipedia.org/wiki/Database_engine" rel="noopener noreferrer"&gt;storage-engine&lt;/a&gt; for Mysql. In this article, we will understand the different isolation levels available in MySQL-InnoDB.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mysql&amp;gt; show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.02 sec)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Isolation level can be set for all sessions, i.e. set globally or set per session. The default transaction isolation level is &lt;code&gt;REPEATABLE-READ&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There are 4 levels of transaction isolation available in MySQL.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;READ-UNCOMMITTED&lt;/strong&gt;: As the name suggests, transactions can read data that are not even committed to the database leading to dirty reads.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fbuildatscale.tech%2Fcontent%2Fimages%2F2021%2F07%2FScreenshot-2021-07-10-at-11.10.14-PM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fbuildatscale.tech%2Fcontent%2Fimages%2F2021%2F07%2FScreenshot-2021-07-10-at-11.10.14-PM.png" alt="https://buildatscale.tech/content/images/2021/07/Screenshot-2021-07-10-at-11.10.14-PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we started 2 sessions, set the isolation level to &lt;code&gt;READ-UNCOMMITTED&lt;/code&gt; in both session, and we can see, that in session 1 we can read data which is not committed by session2.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;READ-COMMITTED&lt;/strong&gt;: In this level, dirty-reads are not possible, as only committed data can be read in select, however, each subsequent read can be &lt;code&gt;non-repeatable&lt;/code&gt;. Let's see examples to understand.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fbuildatscale.tech%2Fcontent%2Fimages%2F2021%2F07%2Fread_committed.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fbuildatscale.tech%2Fcontent%2Fimages%2F2021%2F07%2Fread_committed.png" alt="https://buildatscale.tech/content/images/2021/07/read_committed.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we can see, that only committed writes were selected, which fixes the dirty-read problem, however, each read would result in different results if other transactions are committed, this leads to &lt;code&gt;non-repeatable&lt;/code&gt; reads.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;REPEATABLE-READ&lt;/strong&gt;: This is the default transaction isolation level of Innodb. In this isolation level, select yields the same value as a snapshot of the first execution of &lt;code&gt;SELECT&lt;/code&gt; is taken and that is used until the transaction ends. With this both dirty-read and non-repeatable reads are handled, however, we may get &lt;a href="https://dev.mysql.com/doc/refman/8.0/en/innodb-next-key-locking.html" rel="noopener noreferrer"&gt;phantom rows&lt;/a&gt; with repeatable-read isolation levels.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fbuildatscale.tech%2Fcontent%2Fimages%2F2021%2F07%2FScreenshot-2021-07-11-at-10.50.35-AM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fbuildatscale.tech%2Fcontent%2Fimages%2F2021%2F07%2FScreenshot-2021-07-11-at-10.50.35-AM.png" alt="https://buildatscale.tech/content/images/2021/07/Screenshot-2021-07-11-at-10.50.35-AM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we started transactions in both the sessions, we can see, that even if the write has been committed to the database, all reads in transaction 1 are still yielding the same result.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fbuildatscale.tech%2Fcontent%2Fimages%2F2021%2F07%2FScreenshot-2021-07-11-at-10.54.54-AM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fbuildatscale.tech%2Fcontent%2Fimages%2F2021%2F07%2FScreenshot-2021-07-11-at-10.54.54-AM.png" alt="https://buildatscale.tech/content/images/2021/07/Screenshot-2021-07-11-at-10.54.54-AM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we see that transaction 1, now has 1 new row which is also a &lt;code&gt;phantom-row&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SERIALIZABLE:&lt;/strong&gt; This isolation level is the strongest possible isolation level, but this also increases the chances of locking conditions. This is done by use of shared read locks and exclusive write locks. SERIALIZABLE is similar to REPEATABLE READ with the additional restriction that the row selected by one transaction cannot be changed by another until the first transaction finishes. Let's see an example.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fbuildatscale.tech%2Fcontent%2Fimages%2F2021%2F07%2FScreenshot-2021-07-11-at-11.46.27-AM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fbuildatscale.tech%2Fcontent%2Fimages%2F2021%2F07%2FScreenshot-2021-07-11-at-11.46.27-AM.png" alt="https://buildatscale.tech/content/images/2021/07/Screenshot-2021-07-11-at-11.46.27-AM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, we can see that when we tried to insert a new row in different transactions, &lt;code&gt;Lock wait timeout exceeded&lt;/code&gt; error. This guarantees the fix from phantom-rows.&lt;/p&gt;

&lt;p&gt;And there you have it, all 4 transaction isolation levels explained with examples. We have also seen that the isolation level is a balance between consistency and throughput. High consistency (SERIALIZABLE) means more locking conditions/less throughput and vice-versa.&lt;/p&gt;

&lt;p&gt;To read more, please visit: &lt;a href="https://buildatscale.tech/transaction-isolation-level-in-innodb/" rel="noopener noreferrer"&gt;https://buildatscale.tech/transaction-isolation-level-in-innodb/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mysql</category>
      <category>transaction</category>
      <category>isolation</category>
      <category>innodb</category>
    </item>
    <item>
      <title>How to structure your code in django</title>
      <dc:creator>Rahul Mishra</dc:creator>
      <pubDate>Thu, 29 Apr 2021 07:06:27 +0000</pubDate>
      <link>https://dev.to/rahul91/how-to-structure-your-code-in-django-16c6</link>
      <guid>https://dev.to/rahul91/how-to-structure-your-code-in-django-16c6</guid>
      <description>&lt;p&gt;&lt;a href="https://medium.com/healthify-tech/django-things-i-wish-i-knew-before-using-it-part-i-5af75beb1480"&gt;https://medium.com/healthify-tech/django-things-i-wish-i-knew-before-using-it-part-i-5af75beb1480&lt;/a&gt;&lt;/p&gt;

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