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.
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 models.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
from django.db import models # Create your models here. class Person(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) class MyPerson(Person): class Meta: proxy = True def fullName(self): return self.first_name + " " + self.last_name |
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.
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_myperson_person.py - Create model Person - Create proxy model MyPerson |
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_myperson_person... OK |
Now let’s just add a data using django shell, first you need to open django shell and add the data.
1 |
python manage.py shell |
And add the data
1 2 3 4 5 |
In [1]: from MyApp.models import Person, MyPerson In [2]: p = Person(first_name = "Codeloop", last_name = "org") In [3]: p.save() |
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.
1 2 3 4 |
In [7]: myperson = MyPerson.objects.all()[0] In [8]: myperson.fullName() Out[8]: 'Codeloop org' |
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