Django Abstract Base Class Model Inheritance

This is our twenty second tutorial in Django, in this tutorial we are going to learn about Django Abstract Base Class Model Inheritance. there are different types of Model Inheritance in Django, but we are going to just talk about Abstract Base Class Model Inheritance , also if you 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 

 

 

 

Python GUI Development Tutorials

1: PyQt5 GUI Development Tutorials

2: Pyside2 GUI Development Tutorials

3: wxPython GUI Development Tutorials

4: Kivy GUI Development Tutorials 

5: TKinter GUI Development Tutorials

 

 

 

 

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 Abstract Base Class Model Inheritance ?

According to Django Documentation Abstract base classes are useful when you want to put some common information into a number of other models. You write your base class and put abstract=True in the Meta class. This model will then not be used to create any database table. Instead, when it is used as a base class for other models, its fields will be added to those of the child 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.

 

 

 

 

After creating of the Django Project, you need to migrate your project. make sure

that you have changed the directory to the created 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 in this model the ContactInfo model cannot be used as a normal Django model,

since it is an abstract base class. It does not generate a database table or have a manager,

and cannot be instantiated or saved directly. but our Customer and Staff models will have 4 fields.

because the three fields are extended from our ContactInfo model.  and there will not be any database table for ContactInfo model, but for Customer and Staff we have database tables.

 

 

 

 

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 just two database models, we don’t have database

model for our ContactInfo model.

 

 

 

 

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 we have just our two models,

and if you open the model you will see four fields.

Django Abstract Base Class Model Inheritance
Django Abstract Base Class Model Inheritance

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

1 thought on “Django Abstract Base Class Model Inheritance”

  1. Hi, first, great article. Second I would like to know how to send/order the fields of the abstract class at the end of the child class. Any hint are more than appreciated.

Comments are closed.

Share via
Copy link
Powered by Social Snap
×