Django Multi Table Model Inheritance

This is our twenty third tutorial in Django, in this tutorial we are going to learn about Multi Table Model Inheritance in Django. in the previous tutorial we have learned about  Django Abstract Base Model Inheritance, you can read the article in the given link. also  if your are interested in Flask Web development, you can check the below links.

 

 

 

 

Flask Web Development Tutorials

1: Flask CRUD Application with SQLAlchemy 

2: Flask Creating News Web Application 

3: Flask Creating REST API with Marshmallow 

 

 

 

 

What is Django Model Inheritance ? 

Model inheritance in Django works almost identically to the way normal class inheritance

works in Python, but with a difference that in Django Model Inheritance you need to

extends from django.db.models.Model.

 

 

 

What is Django Multi Table Model Inheritance ?

According to Django Documentation this is the second type of  model inheritance

supported by Django. and it is used when each model in the hierarchy is a model

all by itself. Each model corresponds to its own database table and can be queried

and created individually. The inheritance relationship introduces links between the

child model and each of its parents (via an automatically-created OneToOneField).

by using this model inheritance, we have tables for our base class and also derived class.

 

 

 

 

So now we need to create a New Project in Django, also for django installation and creating New Project you can read this article Django Introduction & Installation. but you can use this command for creating of the New Project.

 

 

 

So after doing this, let’s create our Django App, i have already talked about Django App in one of my articles, you can check this Django Apps & URL Routing.

 

 

 

After creation of the App, you need to add the created App in your settings.py file

INSTALLED_APP section.

 

 

 

Now open your models.py in your MyApp App, and add your model.

 

So you can see that we have two models, when we do migrations, there will be database tables for our both models, we have a database table for Place and also Restaurant. 

All of the fields of Place will also be available in Restaurant, although the data will reside in a different database table.

 

 

 

After creating of the models, we need to migrate our project, first you need to do

migrations and after that migrate.

 

 

 

You can see that Django created two database models for us.

 

 

 

Now you need to migrate.

 

 

 

This is the output in the terminal.

 

 

 

 

Also you can add your Model in Django Admin Panel, so first of all create a superuser, you can read this article for creating Django SuperUser Django Admin Panel , after that open your admin.py file and add your models.

 

 

 

 

Now run your Django Project.

 

 

 

Go to http://localhost:8000/admin/, you can see that the Place model fields are available in our Restaurant Model.

Django Multi Table Model Inheritance
Django Multi Table Model Inheritance

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Share via
Copy link
Powered by Social Snap
×