Complete Guide on Django ORM | Django Tutorials
June 14, 2021 2023-04-13 13:21Complete Guide on Django ORM | Django Tutorials
Complete Guide on Django ORM | Django Tutorials
Create a Django Model | Django ORM
We have understood what is Django ORM (object-relational mapper). In this section, we will learn to create a Django model. Actually, we have learned in the Django models tutorial in which, we have briefly discussed with examples, you can read. But don’t worry, here we also discuss with our beginner readers. Let’s start!
You have to open the models.py file of your application folder. In that file, you have to define a model class with the name for which you want to create a model. It is important to know that your model class name should be an entity name.
Do you know what is Entity?
The entity is the real world things (person, place, things, concept, or event)
If you don’t knowledge about the entity, then you should give a name to the model class that should have some properties or attributes for example student model class will have a name, father name, email, city, subject as its attributes.
Example
In this example, we are talking about a Car that is an entity. Because Car has some attributes or properties as car name, model number, price, color, etc. All the below lines of code are attributes related to entity Car.
In models.py file
from django.db import models
After creating the above model class you need to run the following command:
python manage.py makemigrations
It will create SQL coding for the above model class in the migrations file in your application folder
Then run another command which will create a database table according to your SQL coding that is generated according to your model class.
python manage.py migrate
In this article, actually, we want to show that what is Django ORM. But other tutorials like working with ORM, CRUD operations, commands to work with Database, etc these are all separate tutorials, we will work on it for our beginners.
To insert the record in the database table you can create a ModelForm and using Django Shell you can add records also. The best way for you to learn Django ModelForm.
What is Queryset | Django ORM
Creating an object
Note: You can create an object if your database table has some records.
As we have learned, an application can interact with a database through QuerySet API. The record we get from the database through query set we say it as an object. You know in the table, there are a set of objects are stored in one table. So we can get a specific object from a specific table or set of objects also. So following command is used to get one object from the table.
Entry.objects.all()[:1].get()
Entry is the table name, objects mean all are the objects in the table, all() means we want to get the first record from all objects. :1 means only one record and get() means we want to get only one record.
The above line of code will get a record, here we have used slicing techniques. If you don’t know about slicing, we will cover it very soon.
To understand all the queries used in Django, we will learn step by step in another Django tutorial. In this tutorial, we are just introducing, so don’t worry we will create comprehensive lessons on QuerySet.
Creating a set of object
Now in this section, we will use a command which will give all the objects are stored in a specific table. So you can show all the records to the user.
Entry.objects.all()
You know, Entry is the table name, it should capital one. The word ‘objects’ is showing all the objects that are stored in a table and all() means we want to get all the objects.
Conclusion
If you have any questions in your mind while reading out the tutorial, then must share them with us. We will provide you the best solution possible.
If you use Facebook, Twitter, Pinterest, LinkedIn, or other social media platforms, then share this Django tutorial, if you share, more students will take the chance to learn.
Recommended Django Tutorial for Beginners
Django Tutorial for Beginners | Django Tutorials seriesÂ
Django Include and Extends Tags in Django Template language
What is Django URL Tag in Django Template Language
FULL Guide | Django messages Framework
Django Form Validation | Complete Django Tutorials
Â