Django Proxy Model Inheritance

This is our twenty fourth tutorial in Django, in this tutorial we are going to learn about Django Proxy Model Inheritance, in the two previous tutorials of Model Inheritance, we have learned about two types of Model Inheritance in Django. you can read the articles in the below links.

 

 

Django Model Inheritance

1:  Django Abstract Based Model Inheritance 

2: Django Multi Table Model Inheritance 

 

 

 

 

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 Proxy Model Inheritance ?

Proxy model is a type of Django Model Inheritance, which you can extends from the base

class and you can add your own properties except fields. You can create, delete and

update instances of the proxy model and all the data will be saved as if you were using

the original (non-proxied) model. The difference is that you can change things like

the default model ordering or the default manager in the proxy, without having to

alter the original. Proxy models are declared like normal models. You tell Django

that it’s a proxy model by setting the proxy attribute of the Meta class to True.

The usage of the Proxy Model is overriding the main functionality of the existing model.

 

 

 

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 models.

 

The MyPerson class operates on the same database table as its parent Person class.

In particular, any new instances of Person will also be accessible through MyPerson,

and vice-versa:

 

 

 

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.

 

 

 

Now let’s just add a data using django shell, first you need to open django shell and add the data.

 

 

 

And add the data

 

 

After adding the data, now we can access the first_name and last_name through

the fullName() method of MyPerson class that we have our proxy model.

 

You can see that first we have got the data from the MyPerson class and after that

we have printed the fullName() method. 

 

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Share via
Copy link
Powered by Social Snap
×