Django Models – Learn to Create Your First Django Model

In this tutorial, we will be covering the Django Models concept. We learned about the Django MTV architecture in the previous tutorial and also got a good idea of the Model Component. Here we will learn about this particular component, model in-depth.

These concepts are very important for beginner web developers, as this will also help you understand other technologies.

Django Models

Introduction to Django Models

So, what are Django Models exactly? A simple answer would be-

A Model in Django is a class imported from the django.db library that acts as the bridge between your database and server. This class is a representation of the data structure used by your website. It will directly relate this data-structure with the database. So that you don’t have to learn SQL for the database.

Also, every database implements SQL in a different way, because they all are made to complete a different task.

This creates a bit of a problem for the developers, for example:

You may know MySQL very well, but what will you do if your task can only be achieved by MongoDB?

There you can’t invest your time in understanding the new database and also trying to figure out how to integrate your server with the database.

This problem is overseen by Django ORM, which is a great tool and provides you with the ability to write python data-structures instead of a database language. Thus making your work much easier.

As you can see the two data structures/ datasets, which one do you think is more readable? Python is my answer.

Django Models - SQL & Django ORM Commands example

This makes it clear for someone who knows Python well and shows the importance of Django ORM over the SQL.

When you create a model, Django executes SQL to design a corresponding table in the database (as shown below) without the need to write even a single line of SQL. Django prefixes the name of the table with your Django application name. Also, the model links related information in the database.

Django Models - Table Example in database

You can also retrieve the data in Python and that gives you all the functionalities of Python, that you will ever need. Also, if you are wondering where the model python code is, it’s in the models.py file in the custom application you have created.

Django Models - models.py file in custom application

This file is only importing the models file of the django.db library

Django Models - file importing the models file
Here, you can create objects of class Model as we have imported the required file. We will create our website on the basis of a dataset which we will define here.

Explore Unique Django Features in detail

Creating Your First Django Model

Let’s do it step by step, also we are assuming that you have a database connected to your server or project otherwise nothing written here will work.

We are going to create a student model where we will have fields like roll_no., name, class and department.

1. Firstly, create a new app by this command:

django-admin startapp student

2. Open the models.py file in that and write this code in it

from django.db import models

Django Models - Models.py Option

class Student(models.Model):
roll_no = models.TextField()
name = models.TextField(max_length = 40)
stud_class = models.TextField()
department = models.TextField()

Django Models - Commands for student details

Here you can see that the models class has lots of methods for fields corresponding to web input. We are using text fields here as they are easier to implement and takes in any input.

3. Now, execute these commands

python manage.py makemigrations
python manage.py migrate

Make sure that you have the database connected and you have started the Apache server and MySQL server in the database.

Learn to perform Python Database access.

4. Open the localhost/phpmyadmin/

Django Models - db structure server screen

5. Now open the main database that you have created for this Django project. Then, check the table student_student

You will see these fields.

Django Models - Student details fields

And all your tables can be made just like that, you can insert the data from there itself.

Advantages of Django Models

A model as stated in the definition is the link between the server and your database. Now, whenever you need the data or any operation is performed where data from the server is needed which is essentially just retrieving data from your database, it will need some middleware or bridge which can convert that data in a transmittable/Http response or more generally a web-transmittable format. There, Model comes in and does this important work for you.

If you don’t use the Django models, then you will have to perform a lot of work manually, like writing code that can connect your database with the Django server. In this case, you have to be an expert programmer. So our recommendation is that you shouldn’t bother yourself whether you master any database or not.

Model not only retrieves the data and converts it into the desirable format but execute it by applying business logic, or the logical part/ backend of your website that is actually inside the model component.

Not every operation is executable in the database and therefore we need backend which can actually perform some operation on our retrieved data, in this case, that language is Python.

So, what is different in a Django Model, you will ask?

Well, a lot of things. We will focus on main things though, for now.

  • Power of Python

If you are familiar with python and its capability to let you create advanced data structures with ease, then you will be pretty confident in using models.

Now, imagine that you have a built-in tool that can turn that python code which you have written directly into any database of any technology with modifying some part in our settings.py file of our project.

Django Models - Part of settings.py file

Here in this part of settings.py, you just have to modify the values of the dictionary. Here the default database which Django provides you is SQLite and after running these 2 commands in your command line,

python manage.py makemigrations
python manage.py migrate

you must have gotten a new file in the project folder by the name db.sqlite3. We write it in the ‘NAME’ index of the dictionary.

Django Models - dbsqlite3 file

Although SQLite is also a nice database letting you use almost all the basic database functionalities, but since we want the website to be scalable, we will be using SQL.

Note:

You can still use any database as essentially, we will be writing python code. That’s the beauty of Django as it supports almost all the databases out there with having built-in support for all of them. There are files which you have to import and we are ready to go.

  • Built-in support for almost all the databases

Django’s “Batteries included” approach triumphs here. As the vast support for the databases is there, even we can use cloud databases which is still a lacking feature in many of the web frameworks.

Likewise, you can very easily change the database to the MySQL by just adding some new indexes in the database dictionary in the image.

Check out Top Django Books for improving Django Web Development knowledge

Summary

In this tutorial, we covered what is a Django model in-depth and how Django Models are different and also learned how we can create and implement models.

Hope, you understood the main need of the Model and the concept behind it which you will be able to use while learning further web-technologies other than Django.

Now, are you clear with the concept of Django Models? If you have any doubts, just enter in the comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.