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.
1 |
django-admin startproject MyProject |
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.
1 |
python manage.py startapp MyApp |
After creation of the App, you need to add the created App in your settings.py file
INSTALLED_APP section.
1 2 3 4 5 6 7 8 9 |
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'MyApp', ] |
Now open your models.py in your MyApp App, and add your model.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
from django.db import models # Create your models here. class Place(models.Model): name = models.CharField(max_length=30) address = models.CharField(max_length=30) def __str__(self): return self.name class Restaurant(Place): serves_tuna = models.BooleanField(default=False) serves_pizza = models.BooleanField(default=False) def __str__(self): return self.serves_tuna |
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.
1 |
python manage.py makemigrations |
You can see that Django created two database models for us.
1 2 3 4 |
Migrations for 'MyApp': MyApp\migrations\0003_place_restaurant.py - Create model Place - Create model Restaurant |
Now you need to migrate.
1 |
python manage.py migrate |
This is the output in the terminal.
1 2 3 4 |
Operations to perform: Apply all migrations: MyApp, admin, auth, contenttypes, sessions Running migrations: Applying MyApp.0003_place_restaurant... OK |
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.
1 2 3 4 5 6 |
from django.contrib import admin from .models import Place, Restaurant # Register your models here. admin.site.register(Place) admin.site.register(Restaurant) |
Now run your Django Project.
1 |
python manage.py runserver |
Go to http://localhost:8000/admin/, you can see that the Place model fields are available in our Restaurant Model.
Subscribe and Get Free Video Courses & Articles in your Email