Databases are one of the most popular technologies being used for the collection and organization of data as it allows the data to be easily accessible, manageable, and updated as well. However, these databases require a management system for them to perform these tasks. Mostly, the language SQL is used to perform operations in a database, however, as your application grows and become more complex, it becomes extremely difficult to have an idea as to what exactly each operation is doing. This is where the technique Object Relational Mapping (ORM) comes into the picture. This allows query and manipulation of the data using an object-oriented programming language of your choice. ORMs reduce the complexity of your code and make it more understandable, which, in turn, makes it easier to update, maintain, and reuse the code.
In this article, we will be taking a look at the Django ORM, which is a Python-based ORM and therefore, one of the most popular technologies being used these days.
What is Django?
Before we move on to looking at the ORM of Django, let us first see what actually this Pythonic technology called Django is.
Django is a free and open-source web framework designed in Python, and therefore, has a very clean and neat design along with it being simple, flexible, reliable, and scalable. It makes things extremely easy for web developers as it provides users with ready-made components which, in turn, prevents them from writing everything from scratch, and as a result, makes their work faster and reduces the overhead on their website. In addition to this, it is extremely secure and helps users avoid security concerns such as UI redress attacks, SQL injections, and so on. It also has an extremely large community that is always accessible through forums and always ready to offer its help to others.
Let us now finally look at the Django ORM and some of its main features.
Accessing the Django ORM
After installing Django and setting up its project, we usually are provided with the following initial files:
mysite over here refers to the name of the project that you created. All these files have their own uses and it is important that one needs to knows what role each file plays. Our focus over here is going to be on the manage.py file which is going to control a lot of different things for us, such as setting up a server, making migrations, communicating with a database, as well as enter ORM mode.
To open the Django ORM, open a command-line from the main directory of your Django project and run the following command:
This will open an interactive shell for us which will allow us to start interacting with the database using the ORM.
Manipulating Database using Queries in Django ORM
Since the ORM allows us to interact with the database, we can now write different queries to retrieve and manipulate the data from the database. However, before we can begin working on the database in the shell, we first have to import all the models associated with it. This can be done by simply running a command in the interactive shell, as shown below:
Over here, the appName refers to the name of your app which you have created and hence where your models are currently stored. The modelName refers to the name of the model that you want to import and use. You can have multiple models imported over here, as seen in the example below:
Now, you can access the model object and read data from it. For example, if we want the list of all the posts, we can simply get them by running the following command into our terminal:
The following is the result of the above command:
We can do several other kind of stuffs in the ORM like creating new database data, updating the data, and all the other database commands that you can.
One of the best things that the Django ORM provides to its users is the ability to automatically link and establish relations between the attributes of your model’s object and the corresponding table fields. In databases, mainly three types of relationships exist. these are the One-to-One relationship, the One-to-Many or Many-to-One relationship, and the Many-to-Many relationships.
A One-to-One relationship is, as the name suggests, where the record of one table corresponds to a single record of another table. In Django ORM, we can easily establish this like the following:
user = models.OneToOneField(
name_of_father = models.CharField(max_length=100)
name_of_mother = models.CharField(max_length=100)
Over here, each user can only have single biological parents, and therefore, it is a one-to-one relationship. Now, if we delete any user accessing this model, it will also delete the model of the 2nd user since they are dependent on each other.
A one-to-many or many-to-one refers to a relationship where a parent record can have several child records, however, it can have only one or none child as well. In Django ORM, we can easily establish this relationship using the ForeignKey field:
name = models.CharField(max_length=255)
customer = models.ForeignKey(
As seen in the above code, a customer can have multiple vehicles.
Lastly, a many-to-many relationships define a relationship where multiple tables can relate to each another. We can create this using the ManyToMany field. In the example below, we have created two models, one for the user and the other for their posts. There can be multiple users as well since each user can have multiple posts.
post = models.ManyToManyField(Post, blank=True)
friends = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True)
post = models.TextField()
likes = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True, related_name='user_likes')
The Django ORM is an extremely powerful tool and has made the work of web developers so much easier. It has a variety of features such as manipulation of database models, establishing a relationship between the models, and so much more. In a nutshell, the Django ORM is one of the best things that comes with Django and is highly effective at the job provided to it.