

{"id":68481,"date":"2019-09-24T12:19:15","date_gmt":"2019-09-24T06:49:15","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=68481"},"modified":"2021-06-21T12:19:17","modified_gmt":"2021-06-21T06:49:17","slug":"django-orm-tutorial","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/django-orm-tutorial\/","title":{"rendered":"Django ORM Tutorial &#8211; The concept to master Django framework"},"content":{"rendered":"<p>Django ORM has been mentioned in all of the database &amp; model tutorials. It is an amazing implementation to increase development speed. Yes, there are other ORMs and some people don\u2019t even like ORMs. No worries!! If you didn\u2019t understand what an ORM is. We will cover that in this tutorial.<\/p>\n<p>The other important topic is of Querysets. These are an important part of ORMs in general. We have used Querysets many times in our tutorials. In this Django ORM tutorial, Querysets are also explained from the very basics. Let\u2019s get started.<\/p>\n<p>1. ORMs<\/p>\n<ul>\n<li>What is an ORM?<\/li>\n<li>The Problem Solved by an ORM<\/li>\n<li>Django ORM<\/li>\n<\/ul>\n<p>2. Querysets in Django<\/p>\n<p>3. Different Relationships between Fields<\/p>\n<ul>\n<li>One to One Relationship<\/li>\n<li>One to Many Relationships<\/li>\n<li>Many to Many Relationships<\/li>\n<\/ul>\n<p>4. Conclusion<\/p>\n<ul>\n<li>The Problems with Django ORM<\/li>\n<\/ul>\n<h2>Django ORM Tutorial<\/h2>\n<h3>1. What is an ORM?<\/h3>\n<p>ORM is an acronym for the <strong>object-relational mapper<\/strong>. The ORM\u2019s main goal is to transmit data between a relational database and application model. The ORM automates this transmission, such that the developer need not write any SQL.<\/p>\n<p>ORM, as from the name, maps objects attributes to respective table fields. It can also retrieve data in that manner.<\/p>\n<p>This makes the whole development process fast and error-free.<\/p>\n<p style=\"text-align: center\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Django-ORM-Tutorial.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-70253 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Django-ORM-Tutorial.jpg\" alt=\"Django ORM Tutorial\" width=\"802\" height=\"420\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Django-ORM-Tutorial.jpg 802w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Django-ORM-Tutorial-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Django-ORM-Tutorial-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Django-ORM-Tutorial-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Django-ORM-Tutorial-520x272.jpg 520w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/a><\/p>\n<p>In the above image, we have some <a href=\"https:\/\/data-flair.training\/blogs\/python-object\/\"><em><strong>Python objects<\/strong><\/em><\/a> and a table with corresponding fields. The object\u2019s attributes are stored in corresponding fields automatically. An ORM will automatically create and store your object data in the database. You don\u2019t have to write any SQL for the same.<\/p>\n<h3>The Problem Solved by ORM<\/h3>\n<p>ORMs have certain benefits over the traditional approach. The main advantage ORMs provide is rapid development. ORMs make project more portable. It is easier to change the database if we use ORMs.<\/p>\n<p style=\"text-align: center\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/django-ORM-vs-SQL.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-70039\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/django-ORM-vs-SQL.png\" alt=\"django ORM vs SQL\" width=\"1251\" height=\"876\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/django-ORM-vs-SQL.png 1251w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/django-ORM-vs-SQL-150x105.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/django-ORM-vs-SQL-300x210.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/django-ORM-vs-SQL-768x538.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/django-ORM-vs-SQL-1024x717.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/django-ORM-vs-SQL-520x364.png 520w\" sizes=\"auto, (max-width: 1251px) 100vw, 1251px\" \/><\/a><\/p>\n<p>In the past, web developers needed to have knowledge of databases too. A database has been an important component from the start. The programming languages used for web development use classes and object for data-interpretation. The class is used to a defined data structure in web-applications. Then the same database schema is created in the database. This task requires skill and knowledge of SQL.<\/p>\n<p>Knowing SQL is also not enough since SQL implementations slightly differ from one another in different databases. This became a difficult and time-consuming task. So, to resolve this, the concept of ORM was introduced in web frameworks. ORMs automatically create a database schema from defined classes\/ models. They generate SQL from <em><strong><a href=\"https:\/\/data-flair.training\/blogs\/python-syntax-semantics\/\">Python code<\/a><\/strong><\/em> for a particular database. ORMs let the developer build the project in one language that means Python.<\/p>\n<p>This increased the popularity of ORMs and web-frameworks. There are different ORMs available in the market but one of the best is Django ORM.<\/p>\n<p style=\"text-align: center\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/different-orm-connectors-editing.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-70254\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/different-orm-connectors-editing.png\" alt=\"different orm connectors - Python Django ORM\" width=\"559\" height=\"137\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/different-orm-connectors-editing.png 559w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/different-orm-connectors-editing-150x37.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/different-orm-connectors-editing-300x74.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/different-orm-connectors-editing-520x127.png 520w\" sizes=\"auto, (max-width: 559px) 100vw, 559px\" \/><\/a><\/p>\n<p>Not only ORMs, but there are also multiple Python Connectors for databases available. These ORMs use connectors to connect databases with a web application. you have to install the connector of a specific database you want to work with. We will be working with MySQL databases.<\/p>\n<h2>Introduction to Django ORM<\/h2>\n<p>Django is shipped with its own ORM. It is a very efficient ORM and is tightly coupled with the Django framework. Django ORM is suitable for handling low-medium complexity queries. Although, some consider <strong>SQLAlchemy ORM<\/strong> to be a better option. The <a href=\"https:\/\/data-flair.training\/blogs\/django-migrations-and-database-connectivity\/\"><em><strong>migrations feature by Django<\/strong><\/em><\/a> is also a part of Django ORM. The ORM has many benefits engulfing those of migrations and Querysets. Django is the only framework which is complete in itself.<\/p>\n<h3>Is it necessary to use ORM?<\/h3>\n<p>This is totally a developer\u2019s choice. Although there are certain benefits of ORM, there are some downsides too. It is beneficial for developers as it allows faster development. Sometimes ORMs generate more complex queries then they should. This can result in a performance decrease. Therefore, it is totally the developer\u2019s choice and the level of work they are doing. <strong>For example &#8211;<\/strong> Django ORM is efficient for working with low-medium complexity models.<\/p>\n<h3>Django ORM vs SQLAlchemy?<\/h3>\n<p>In most cases, people prefer SQLAlchemy over Django ORM. The reason being SQLAlchemy is more adept to work with high-complexity data structures. Django ORM is powerful in its own way. The ORM provides features integrated with Django which are more important than just performance improvement.<\/p>\n<p>Currently, Django ORM is not swappable with SQLAlchemy. There are some hacks to do it but it&#8217;s unofficial. Django\u2019s future releases are expected to provide this functionality. Also, both SQLAlchemy and Django ORM is actively developed.<\/p>\n<p>So, there is no clear winner and it totally depends on the type of project you are working on.<\/p>\n<h3>2. Querysets in Django<\/h3>\n<p>We all use queries to retrieve data from the database. Querysets are Django\u2019s way to retrieve data from the database. The Django ORM lets us use Querysets.<\/p>\n<p>A Queryset is a list of objects of a model. We use Querysets to filter and arrange our data. These make our work as a Python developer easier.<\/p>\n<p>We will be practicing different implementations of Querysets. We have discussed most of them in our previous articles. So, you can check them out from DataFlair&#8217;s <em><strong><a href=\"https:\/\/data-flair.training\/blogs\/django-tutorials-home\/\">Django Tutorial Series<\/a><\/strong><\/em>. Querysets are generally associated with CRUD applications. We will be discussing that in a separate tutorial.<\/p>\n<p>In this tutorial, we will be focusing more on Django ORM.<\/p>\n<h3>Different Relationships between Fields<\/h3>\n<p>Django ORM provides a level of abstraction which makes it easy to work with objects. ORM will automatically relate the object\u2019s attributes to corresponding table fields. We are going to make some models and check their field relationships in other tables.<\/p>\n<p>It is expected that you can make models and applications. This Django ORM tutorial is an advanced concept and basics of Django is a prerequisite.<\/p>\n<p>Now, you can create models by pasting the code in models.py of your application. Let\u2019s get started.<\/p>\n<h4>1. One to One Relationship<\/h4>\n<p>A one-to-one relationship exists between two tables. For each row in table1, there shall be a row\/ entity in table2.<\/p>\n<p>Let\u2019s understand it with an example.<\/p>\n<p style=\"text-align: center\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/one-to-one-relationship.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-70032\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/one-to-one-relationship.png\" alt=\"one to one relationship - Django ORM\" width=\"662\" height=\"456\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/one-to-one-relationship.png 662w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/one-to-one-relationship-150x103.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/one-to-one-relationship-300x207.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/one-to-one-relationship-520x358.png 520w\" sizes=\"auto, (max-width: 662px) 100vw, 662px\" \/><\/a><\/p>\n<p>There are two tables here, Customer and Vehicle. Every customer owns only one vehicle. Thus, a one-to-one relationship exists. Now, paste this code in models.py to implement this.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from django.db import models\r\n\r\n#DataFlair #DjangoTutorials\r\n# Create your models here.\r\nclass Customer(models.Model):\r\n    name = models.CharField(max_length=255)\r\n\r\nclass Vehicle(models.Model):\r\n    name = models.CharField(max_length=255)\r\n    customer = models.OneToOneField(\r\n        Customer,\r\n        on_delete=models.CASCADE,\r\n        related_name='vehicle'\r\n    )<\/pre>\n<p style=\"text-align: left\"><strong>Code Display:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/One-to-One-Relationship-Model-Code-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-70252 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/One-to-One-Relationship-Model-Code-1.png\" alt=\"One to One Relationship Model Code\" width=\"1920\" height=\"976\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/One-to-One-Relationship-Model-Code-1.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/One-to-One-Relationship-Model-Code-1-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/One-to-One-Relationship-Model-Code-1-300x153.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/One-to-One-Relationship-Model-Code-1-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/One-to-One-Relationship-Model-Code-1-1024x521.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/One-to-One-Relationship-Model-Code-1-520x264.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>Now, we will register these models with admin to populate them.<\/p>\n<p style=\"text-align: left\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/register-models-with-admin.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-70226 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/register-models-with-admin.png\" alt=\"register models with admin - Django ORM\" width=\"1920\" height=\"976\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/register-models-with-admin.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/register-models-with-admin-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/register-models-with-admin-300x153.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/register-models-with-admin-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/register-models-with-admin-1024x521.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/register-models-with-admin-520x264.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>Now, create some objects from the Django Admin. First, make some owner objects. It will show a dropdown list of Customer objects in Vehicle.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Customer-objects-in-Vehicles.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-70227 size-full aligncenter\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Customer-objects-in-Vehicles.png\" alt=\"Dropdown list of Customer objects in Vehicles\" width=\"1920\" height=\"943\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Customer-objects-in-Vehicles.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Customer-objects-in-Vehicles-150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Customer-objects-in-Vehicles-300x147.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Customer-objects-in-Vehicles-768x377.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Customer-objects-in-Vehicles-1024x503.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Customer-objects-in-Vehicles-520x255.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>Now, after adding some objects we will check the database.<\/p>\n<div id=\"attachment_70228\" style=\"width: 1930px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Customer-table.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-70228\" class=\"wp-image-70228 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Customer-table.png\" alt=\"Customer Table - Django ORM Tutorial\" width=\"1920\" height=\"948\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Customer-table.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Customer-table-150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Customer-table-300x148.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Customer-table-768x379.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Customer-table-1024x506.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Customer-table-520x257.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><p id=\"caption-attachment-70228\" class=\"wp-caption-text\">Customer Table<\/p><\/div>\n<div id=\"attachment_70229\" style=\"width: 1930px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Vehicle-Table.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-70229\" class=\"wp-image-70229 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Vehicle-Table.png\" alt=\"Vehicle Table - Django ORM\" width=\"1920\" height=\"948\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Vehicle-Table.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Vehicle-Table-150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Vehicle-Table-300x148.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Vehicle-Table-768x379.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Vehicle-Table-1024x506.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Vehicle-Table-520x257.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><p id=\"caption-attachment-70229\" class=\"wp-caption-text\">Vehicle Table<\/p><\/div>\n<p>Now, just focus on Vehicle Table. You can see that it stores the Customer ID which is the same field as ID in the Customer table. This is a one-to-one relationship. Let\u2019s see the <em><strong><a href=\"https:\/\/data-flair.training\/blogs\/sql-tutorials-home\/\">SQL<\/a><\/strong><\/em> we would have to write for the same task.<\/p>\n<p style=\"text-align: left\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Complex-SQL.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-70230 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Complex-SQL.png\" alt=\"Complex SQL Terminal Code - Django ORM Tutorial\" width=\"1920\" height=\"1030\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Complex-SQL.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Complex-SQL-150x80.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Complex-SQL-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Complex-SQL-768x412.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Complex-SQL-1024x549.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Complex-SQL-520x279.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>That is some complex SQL. Django ORM saves us a lot from writing this code and syncing our models. Check out the <em><strong>DataFlair&#8217;s\u00a0Django Migrations Tutorial.<\/strong><\/em><\/p>\n<h4>2. One to Many Relationships<\/h4>\n<p>Just like one to one we can have one-to-many relationships. We are going to use a different model for showcasing this.<\/p>\n<p>A one to many relationships is where one object from table1 can have multiple relations with entities in table2. Although, table2 objects will have only one relation to the object of table1.<\/p>\n<p>Let\u2019s understand this from the example:<\/p>\n<p style=\"text-align: center\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/one-to-many-relationship.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-70034\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/one-to-many-relationship.png\" alt=\"one to many relationship - Django ORM\" width=\"663\" height=\"468\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/one-to-many-relationship.png 663w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/one-to-many-relationship-150x106.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/one-to-many-relationship-300x212.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/one-to-many-relationship-520x367.png 520w\" sizes=\"auto, (max-width: 663px) 100vw, 663px\" \/><\/a><\/p>\n<p>Before developing this model: please make it in a new application.<\/p>\n<p>The <strong>code<\/strong> for this model is:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from django.db import models\r\n\r\n#DataFlair #DjangoTutorials\r\n# Create your models here.\r\nclass Customer(models.Model):\r\n    name = models.CharField(max_length=255)\r\n\r\nclass Vehicle(models.Model):\r\n    name = models.CharField(max_length=255)\r\n    customer = models.ForeignKey(\r\n        Customer,\r\n        on_delete=models.CASCADE,\r\n        related_name='Vehicle'\r\n    )<\/pre>\n<p><strong>Code Display:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/One-to-Many-relationship.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-70231 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/One-to-Many-relationship.png\" alt=\"One to Many relationship code - Django ORM tutorial\" width=\"1920\" height=\"976\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/One-to-Many-relationship.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/One-to-Many-relationship-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/One-to-Many-relationship-300x153.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/One-to-Many-relationship-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/One-to-Many-relationship-1024x521.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/One-to-Many-relationship-520x264.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>Populate the models via admin. Once you have created some objects then check the database.<\/p>\n<div id=\"attachment_70233\" style=\"width: 1930px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Customer-Table.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-70233\" class=\"wp-image-70233 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Customer-Table.png\" alt=\"Customer Table - Django ORM\" width=\"1920\" height=\"948\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Customer-Table.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Customer-Table-150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Customer-Table-300x148.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Customer-Table-768x379.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Customer-Table-1024x506.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Customer-Table-520x257.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><p id=\"caption-attachment-70233\" class=\"wp-caption-text\">Customer Table<\/p><\/div>\n<div id=\"attachment_70234\" style=\"width: 1930px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Vehicle-Table-2.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-70234\" class=\"wp-image-70234 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Vehicle-Table-2.png\" alt=\"Vehicle Table - Django ORM\" width=\"1920\" height=\"948\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Vehicle-Table-2.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Vehicle-Table-2-150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Vehicle-Table-2-300x148.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Vehicle-Table-2-768x379.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Vehicle-Table-2-1024x506.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Vehicle-Table-2-520x257.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><p id=\"caption-attachment-70234\" class=\"wp-caption-text\">Vehicle Table<\/p><\/div>\n<p>From this table, we can see that one customer can have more than one vehicle.<\/p>\n<h4>3. Many to Many Relationships<\/h4>\n<p>We can also implement many to many relations in the database. In this case, we are taking examples of multiple drivers.<\/p>\n<p style=\"text-align: center\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/many-to-many-relationship.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-70038\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/many-to-many-relationship.png\" alt=\"many to many relationship - Django ORM tutorial\" width=\"663\" height=\"465\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/many-to-many-relationship.png 663w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/many-to-many-relationship-150x105.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/many-to-many-relationship-300x210.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/many-to-many-relationship-520x365.png 520w\" sizes=\"auto, (max-width: 663px) 100vw, 663px\" \/><\/a><\/p>\n<p>Here we have multiple workers for multiple machines. A worker can be assigned to operate more than one machine. Also, a machine can be operated by multiple workers one at a time.<\/p>\n<p>To implement the same in model, paste this code in<strong> models.py<\/strong>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from django.db import models\r\n\r\n# Create your models here.\r\n#DataFlair #Many to Many Relationship\r\nclass Worker(models.Model):\r\n    name = models.CharField(max_length=255)\r\n\r\nclass Machine(models.Model):\r\n    name = models.CharField(max_length=255)\r\n    worker = models.ManyToManyField(\r\n        Worker,\r\n        related_name='Machine'\r\n    )<\/pre>\n<p style=\"text-align: left\"><strong>Code Display:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Many-to-many-relationship.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-70235 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Many-to-many-relationship.png\" alt=\"Many to many relationship code - Django ORM Tutorial\" width=\"1920\" height=\"976\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Many-to-many-relationship.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Many-to-many-relationship-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Many-to-many-relationship-300x153.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Many-to-many-relationship-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Many-to-many-relationship-1024x521.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Many-to-many-relationship-520x264.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>Now, let&#8217;s fill some data in them again. We will now inspect the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Database\">database<\/a>.<\/p>\n<div id=\"attachment_70236\" style=\"width: 1930px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Worker-Table.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-70236\" class=\"wp-image-70236 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Worker-Table.png\" alt=\"Worker Table\" width=\"1920\" height=\"948\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Worker-Table.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Worker-Table-150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Worker-Table-300x148.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Worker-Table-768x379.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Worker-Table-1024x506.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Worker-Table-520x257.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><p id=\"caption-attachment-70236\" class=\"wp-caption-text\">Worker Table<\/p><\/div>\n<div id=\"attachment_70237\" style=\"width: 1930px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Machine-Table.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-70237\" class=\"wp-image-70237 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Machine-Table.png\" alt=\"Machine Table\" width=\"1920\" height=\"948\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Machine-Table.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Machine-Table-150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Machine-Table-300x148.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Machine-Table-768x379.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Machine-Table-1024x506.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Machine-Table-520x257.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><p id=\"caption-attachment-70237\" class=\"wp-caption-text\">Machine Table<\/p><\/div>\n<div id=\"attachment_70238\" style=\"width: 1930px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Many-to-many-relationship-table.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-70238\" class=\"wp-image-70238 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Many-to-many-relationship-table.png\" alt=\"Many to many relationship table - Django ORM Tutorial\" width=\"1920\" height=\"948\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Many-to-many-relationship-table.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Many-to-many-relationship-table-150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Many-to-many-relationship-table-300x148.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Many-to-many-relationship-table-768x379.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Many-to-many-relationship-table-1024x506.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Many-to-many-relationship-table-520x257.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><p id=\"caption-attachment-70238\" class=\"wp-caption-text\">Many to many relationship table<\/p><\/div>\n<p>This is a special case of relationships. Many to Many Relationship requires a separate table. There they store objects related to other objects.<\/p>\n<p>These relationships are common for objects and classes. These can be difficult to write with SQL.<\/p>\n<h2>Summary<\/h2>\n<p>Django ORM and other ORMs are very powerful tools for web-developers. However, there are some issues which cannot be solved by them. ORMs are abstractions and automate a process. There are points where they can generate more complex queries than manual.<\/p>\n<p>It all depends on the developer and their skill with the corresponding database. On the contrary, the ORM exists only for <em><strong><a href=\"https:\/\/data-flair.training\/blogs\/sql-rdbms\/\">SQL and RDBMS (Relational database management systems)<\/a><\/strong><\/em>. There are no ORMs for NoSQL databases. So, it depends on your use case and how you perceive the data.<\/p>\n<p>ORMs have both bad and good sides. Particularly Django ORM is highly favored by web-developers. It&#8217;s not very efficient when compared to SQLAlchemy. The deep integration with the Django framework is where it really shines. Also, Django ORM is an abstraction layer. It provides us with features like Querysets and migrations and these relations.<\/p>\n<p><em><strong>Now, let&#8217;s move on to the next article &#8211; <a href=\"https:\/\/data-flair.training\/blogs\/django-cms\/\">Django CMS<\/a><\/strong><\/em><\/p>\n<p>Share your thoughts and queries regarding the Django ORM tutorial in the comment section below.<span hidden class=\"__iawmlf-post-loop-links\" data-iawmlf-links=\"[{&quot;id&quot;:1389,&quot;href&quot;:&quot;https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/Database&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20251206234424\\\/https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/Database&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-09 06:10:02&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-17 14:28:47&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-21 01:55:02&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-26 10:35:17&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-09 09:19:35&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-12 09:54:50&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-18 13:44:18&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-29 07:16:28&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-02 09:01:49&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-06 10:11:41&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-09 14:45:04&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-12 18:46:32&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-16 10:44:59&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-20 13:55:55&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-23 14:00:48&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-03 05:07:44&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-06 09:43:45&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-10 08:04:58&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-15 19:26:01&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-20 01:22:16&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-27 04:16:45&quot;,&quot;http_code&quot;:429},{&quot;date&quot;:&quot;2026-04-02 01:45:24&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-06 09:47:37&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-10 22:31:00&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-15 11:46:58&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-19 17:07:24&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-25 23:21:42&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-30 13:50:10&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-05 15:10:10&quot;,&quot;http_code&quot;:429},{&quot;date&quot;:&quot;2026-05-12 13:29:11&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-16 02:22:14&quot;,&quot;http_code&quot;:429},{&quot;date&quot;:&quot;2026-05-22 12:26:10&quot;,&quot;http_code&quot;:429},{&quot;date&quot;:&quot;2026-05-27 10:31:24&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-01 02:07:31&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-06-10 23:05:12&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-06-14 13:50:00&quot;,&quot;http_code&quot;:200}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-14 13:50:00&quot;,&quot;http_code&quot;:200},&quot;process&quot;:&quot;done&quot;}]\"><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Django ORM has been mentioned in all of the database &amp; model tutorials. It is an amazing implementation to increase development speed. Yes, there are other ORMs and some people don\u2019t even like ORMs.&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":70253,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19149],"tags":[19567,20952,20951,18570,20950],"class_list":["post-68481","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-django","tag-django-orm","tag-many-to-many-relationships","tag-one-to-many-relationships","tag-one-to-one-relationship","tag-querysets"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Django ORM Tutorial - The concept to master Django framework - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn the complete concept of Django ORM thoroughly with querysets and relationships between fields and the problem solved by ORM &amp; problem with Django ORM.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/django-orm-tutorial\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Django ORM Tutorial - The concept to master Django framework - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn the complete concept of Django ORM thoroughly with querysets and relationships between fields and the problem solved by ORM &amp; problem with Django ORM.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/django-orm-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-09-24T06:49:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-06-21T06:49:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Django-ORM-Tutorial.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Django ORM Tutorial - The concept to master Django framework - DataFlair","description":"Learn the complete concept of Django ORM thoroughly with querysets and relationships between fields and the problem solved by ORM & problem with Django ORM.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/django-orm-tutorial\/","og_locale":"en_US","og_type":"article","og_title":"Django ORM Tutorial - The concept to master Django framework - DataFlair","og_description":"Learn the complete concept of Django ORM thoroughly with querysets and relationships between fields and the problem solved by ORM & problem with Django ORM.","og_url":"https:\/\/data-flair.training\/blogs\/django-orm-tutorial\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2019-09-24T06:49:15+00:00","article_modified_time":"2021-06-21T06:49:17+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Django-ORM-Tutorial.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/django-orm-tutorial\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/django-orm-tutorial\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"Django ORM Tutorial &#8211; The concept to master Django framework","datePublished":"2019-09-24T06:49:15+00:00","dateModified":"2021-06-21T06:49:17+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/django-orm-tutorial\/"},"wordCount":1632,"commentCount":5,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/django-orm-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Django-ORM-Tutorial.jpg","keywords":["Django ORM","Many to Many Relationships","One to Many Relationships","One-to-one relationship","Querysets"],"articleSection":["Django Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/django-orm-tutorial\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/django-orm-tutorial\/","url":"https:\/\/data-flair.training\/blogs\/django-orm-tutorial\/","name":"Django ORM Tutorial - The concept to master Django framework - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/django-orm-tutorial\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/django-orm-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Django-ORM-Tutorial.jpg","datePublished":"2019-09-24T06:49:15+00:00","dateModified":"2021-06-21T06:49:17+00:00","description":"Learn the complete concept of Django ORM thoroughly with querysets and relationships between fields and the problem solved by ORM & problem with Django ORM.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/django-orm-tutorial\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/django-orm-tutorial\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/django-orm-tutorial\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Django-ORM-Tutorial.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/09\/Django-ORM-Tutorial.jpg","width":802,"height":420,"caption":"Django ORM Tutorial"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/django-orm-tutorial\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Django Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/django\/"},{"@type":"ListItem","position":3,"name":"Django ORM Tutorial &#8211; The concept to master Django framework"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/68481","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=68481"}],"version-history":[{"count":10,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/68481\/revisions"}],"predecessor-version":[{"id":72299,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/68481\/revisions\/72299"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/70253"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=68481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=68481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=68481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}