Blog

Django Model Inheritance | Django Tutorial for Beginners

Model inheritance copy
Django

Django Model Inheritance | Django Tutorial for Beginners

Django Model Inheritance

If you have confusion related to Django Model Inheritance, you can learn clearly what is model inheritance actually? This tutorial is very important to learn because it will help you, when you developing a  big project, and you want to develop a database with optimization code. Inheritance is the technique that helps to reduce the code line, which is best for code optimization. If you learn this tutorial, you will be able to apply your project databases, which will reduce the line of code and easy to understand the structure of database tables. So let’s start to learn!

What is Django Model?

Django Model is actually referred to as the database table which has row and column combinations to store data. A website has a number of models to store different types of data. For example, you are developing a website related to School then you have to create a model class to store different data of a school e.g. Teacher, Student, Subject, Sport, Exam, Fees, etc. As you know that there is a specific relation between teacher and student, then you have to make a relationship between them. In a database it has great importance, because we need to link all the tables to each other to store data in such a way that it makes it easy to retrieve, update, delete, etc.

What is Django Model Inheritance?

To understand Django model inheritance, you have to understand what is the inheritance actually? Inheritance is the technique that takes common things from parents. For example, you want to inherit an object from another object. The inherited object will have properties or characteristics of that object from which we have inherited. So this technique is applied in different skills as in object-oriented programming language, we used it to inheriting a class to make a chile class.

So, in terms of the Django Model, what is the Model inheritance? It is the way of creating a model which inheriting the parent model, the child model will have all the properties and characteristics of the parent model. For example, the parent model has attributes of name, fathaer_name, email, so the child Model class will have these attributes.

Types of Django Model Inheritance

There are different types of Django Model inheritance, we will discuss all the types in detail in separate tutorials.

  • Abstract base classes
  • Multi-table inheritance
  • Proxy models

How to Inherit a Django Model

It is a simple example, basic understanding for you, there are different types of inheritance we will discuss in another tutorial. Our purpose is to share basic knowledge of model inheritance with you. We will update this lesson, from time to time.

The model Person is the parent model class which has some attributes and the other two model classes Student and Teacher are the child class.

from django.db import models
# wirte code for your model class.
class Person(models.Model):
    name=models.CharField(max_length=20)
    email=models.EmailField(max_length=20)
    mobile_number=models.TextField(max_length=20)
    address=models.TextField(max_length=20)
    class Meta:
        abstract=True # necessary to add this attribute to make inheritable
class Student(Person):
    grade=models.CharField(max_length=15)
class Teacher(Person):
    salary=models.CharField(max_length=10)
    

Benefits of creating model Inheritance

  • It reduces the line of code to write
  • It makes the code optimization
  • Easy to understand database tables structure
  • Reusability
  • It makes less development time
  • Less cost
  • Avoid writing the same code again and again

Now! what you have to do?

As you have understood this article about “Django Model Inheritance”, now you must follow instructions as discussed in this article. In case of any issues or problems, don’t worry, you can discuss them with us.

On other hand, If you have any suggestions or knowledge about this article, you can share it with us, we will appreciate you!

If this article is good then share it on FacebookTwitterPinterestInstagram, etc.

Advanced Tutorials for Beginners

Django File Uploading | Django Tutorial for Beginners

What is Django Cache | Django Tutorial for Beginners

Django Create superuser | Django Tutorial

How to Getting Started with Python Programming

10+ Top Python Development Companies | Beneficial

Leave your thought here

Your email address will not be published.

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
  • Attributes
  • Custom attributes
  • Custom fields
Click outside to hide the compare bar
Compare
Wishlist 0
Open wishlist page Continue shopping