In this Python Django tutorial, I will explain how to delete schema from Python Django step by step.
Recently, I have been working on a Django e-cart project, and I mistakenly constructed a department schema. So now, I decided to remove this schema from your Django project.
Here we will see:
- What is schema
- Different types of schema
- How to delete schema from Python Django project
I found that the solution is simple and clear, we will delete the model’s schema entirely, or we may say that we remove the model from your Django project.
How to Delete Schema from Python Django
What is schema
Before we proceed to learn the process of deleting the schema from the Django project database. We need to have a little bit of knowledge about the scheme.
The skeleton structure of a database schema is the logical representation of the entire database. It outlines the structure of the data and the relationships that connect them.
It describes the database that can be illustrated using a schema diagram. Database designers create the schema to make the database easier for programmers to comprehend and use.
Read: Python Django vs Pyramid
Types of schema
A database schema can be broadly classified into two types:
- Physical database schema: This schema deals with how data is kept in storage.
- Logical database schema: This outlines all the logical restrictions that must be imposed on the data being stored.
Delete schema from Python Django project
After understanding the schema, the next task is to understand how to delete a scheme from the Django project.
Now, all we have to do is just remove the model class which is unnecessarily defined in the models.py file of the application.
Let’s understand this thing with the help of an example where we will remove the Department model class.
Here, we will have a Django project called EShopProject and an app inside of it called EShopApp.
Additionally, there are two models for this app: Department and Product as shown below.
from django.db import models # Create your models here. class Product(models.Model): product_id = models.IntegerField() name = models.CharField(max_length=150) description = models.TextField(max_length=2000) def __str__(self): return self.name class Department(models.Model): department_id = models.IntegerField() name = models.CharField(max_length=250) def __str__(self): return self.name
Since our objective is to delete the Department model. We must open the models.py file and remove the Department model class from there.
This is how models.py files look now.
from django.db import models # Create your models here. class Product(models.Model): id = models.IntegerField() name = models.CharField(max_length=150) description = models.TextField(max_length=2000) def __str__(self): return self.name
In the above code, we have specified a Model named Product. This Product model consists of 3 attributes that represent 3 table fields for the Product table in Django. Moreover, we have also specified the string representation for Model which will be the name of the product.
Now to affect the changes in the database, use makemigartions and migrate commands as given below.
python manage.py makemigrations
After executing the makemigrations command in Django, we need to execute the migrate command. The migrate command in Django is given below.
python manage.py migrate
After executing both the above-specified command in Django, we have successfully deleted schema from Python Django Project.
This is how we delete the database schema from Python Django.
With this, we have successfully deleted the schema from Django and also get an overview of the schema. Additionally, we have also covered the following topics.
- What is scheme
- Different types of schema
- How to delete scheme from Python Django project
You may also like to read the following Python Django tutorials.
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.