SlideShare a Scribd company logo
1 of 21
Download to read offline
Model Inheritance
                                       ...in Django




This presentation is about a feature of the Django Object Relational Mapping layer called
“Model Inheritance”.
Impedance Mismatch




First some background. The root of the problem that Object Relational Mapping (ORM)
systems attempt to solve is a fundamental “Impedance Mismatch” between the object-
oriented world inhabited by OO programming languages, such as Python, and the relational
database world as defined by RDBMS’ such as PostgreSQL, Oracle and MySQL.

These two worlds have very different ways of looking at how data should be organized.
Object Oriented Values




One of the main tools of the OO world is inheritance. In OO inheritance, a descendant class
inherits the characteristics of its ancestor. This allows common functionality to be
programmed into the ancestor class, and then “specialized” sub-classes can be created,
extending the functionality of the ancestor, or superclass.

In this example, a User and a Customer are different kinds of specialized extensions of a
Person. Additionally, an E-Commerce User inherits from both User and Customer, and
therefore has all of the traits of its superclasses.

It is common to organize data in OO in various hierarchies of superclasses and subclasses.
Relational Values
In the world of relational databases, database tables are defined with “foreign keys” that
relate them to each other. In this example, we see that each row in the User table has a
person_id, which is a foreign key that relates to a record in the Person table.

Data organization is done through relating records together, and then composing queries
that pulls the necessary data from the database, following the relationships defined by these
foreign keys.
ORM Layers are both
             OO and Relational


Because they are designed to bridge the different approach to managing data by the two
worlds, ORMs are inherently both Object Oriented AND Relational. It is exactly what is
advertised by their name: Object Relational Mapping.
Building Inheritance
              into ORMs is difficult
Trying to bring OO-style inheritance to ORMs is difficult, because the relational world doesn’t
really support the concept. It opens a lot of questions of the best way to provide inheritance
functionality to the programmer, in a way that isn’t a total hack on the relational side.
Django approach
                      before now:
                                   Use Composition




Up until now, Django hasn’t really supported model inheritance. When a Django application
developer was presented with a situation that would best be solved with inheritance, they
were advised to use a technique called composition instead.

The most prominent example of this is the user profile. It would be a common case for a
programmer to want to extend the User class that comes in django.contrib.auth so that they
can contain attributes specific to the application. Django historically has solved this with a
“user profile” - a separate class that is identified in settings.py and can be retrieved by the
get_user_profile() method on User objects.
Unique Foreign Key




To use composition, one defines a foreign key that is also unique on the composited class
(the class that would be the subclass if we were solving this problem using OO inheritance).

For example, if we hypothetically wanted to specialize a Person class and make a User, the
User would have a ForeignKey field that pointed back at the Person model (the Django ORM
equivalent of a relational foreign key). In addition, we would designate the field unique,
ensuring that there would always only be one User per Person.

Traits on the composited “superclass” would have to be accessed explicitly - they are not
truly inherited by the specialization class.
This Sucks



There are a number of reasons this is a suboptimal situation.

1. It makes queries more complicated, meaning that the work of defining the relationship
that should be done once in data modeling is now pushed all over the application code.

2. It fails to take advantage of Python’s OO features and thus, power.

3. It creates an object model that doesn’t really best describe the real-world entities that it is
trying model.
Malcolm Tredinnick
This man has come to the rescue, however. This is Malcom Tredinnick, and he has for some
time now been working on a branch of the Django code called QuerySet Refactor.
Queryset Refactor



The QuerySet Refactor branch brings a number of new features to the Django ORM. Most
notably it has brought true Model Inheritance. Despite being difficult to program, the
QuerySet Refactor programmers, led by Malcolm, have managed to implement it in a couple
of great ways.

Furthermore, on April 26th this year, QuerySet Refactor was merged into the Django Trunk.
That means that all of its features are now part of the mainstream development of Django.
Two Approaches to
                Model Inheritance


With the merge complete, Django now offers two different approaches to Model Inheritance.
An application might choose to use both approaches, since they offer advantages in different
circumstances.
1. Abstract Base Classes




The first approach is called Abstract Base Classes. This approach is best used when the
parent superclass is never meant to be instantiated on its own. It is merely a source of
common functionality that will be used by subclasses.

Abstract Base Classes are specified by adding “abstract = True” in the Meta inner class. When
syncdb is run, the Django ORM will not create a database table for these models.

Model classes that extend the Abstract Base Class will automatically inherit fields defined in
the superclass, however. The Django ORM will automatically generate corresponding
columns and relations for the superclass’ fields in the table of the subclass.
Abstract Base Classes
                are a coding
             convenience only.


In the case of Abstract Base Classes, the inheritance relationship is ignored at the relational
level. They are essentially a kind of advanced syntactic sugar - providing a type of “include”
in model definitions. Once the Django ORM has “compiled” the models into SQL, the
Abstract Base Class essentially ceases to exist.
ABC Gotcha
                                    “related_name”




Abstract Base Classes can carry a couple of gotchas. Let’s look at one related to the use of
the “related_name” attribute in ForeignKey fields.
Specifying a ForeignKey
          in an ABC superclass




If we put a foreign key field in an ABC, we might wish to specify a related_name in a
ForeignKey field. The related_name is the name by which this class (“Person”) is known by
the target model of the ForeignKey (“Company”). So a Company object has “people”.

The problem comes when we have more than one concrete subclass of the ABC. We have to
remember that there is no database table that corresponds to Person. Instead, the Django
ORM compiles the fields of the ABC into the table definitions of the subclasses.

This means that Company objects will have a “people” field that points to both User and
Customer objects. Django will throw an error when syncdb is run.
Specifying a ForeignKey
           in an ABC superclass




The suggested solution is to change the related name to something that is dynamically
interpolated. By using the “%(class)s_related” notation, we will create two attributes in the
Company model: “user_related” pointing to users, and “customer_related” pointing to
customers.
2. Multiple Table Inheritance




The second approach to Model Inheritance that Django now provides is called Multiple Table
Inheritance. In this approach, Django generates a separate relational table for each model in
the inheritance hierarchy.

This approach is useful for circumstances where you may wish to instantiate models from
both the superclass and its subclasses. In this example, we may wish to define Persons that
are not Users, in addition to Users that inherit Person attributes.

MTI inheritance can be engaged simply by subclassing an existing model. In this case there
is no “abstract” attribute being set in an inner Meta class.
MTI makes inheritance
        a relationship under the
                  hood.


As mentioned, when the Django ORM generates SQL from models using MTI, a separate table
is created for both the superclasses and subclasses. In addition, the inheritance between
models is converted into a relation.

The subclass table will have a special additional column called “<model_name>_ptr_id”, a
foreign key that points at the superclass table. When an instance of the subclass model is
pulled from the database, the ORM will pull the related row in the superclass table and build
a model instance with data from both tables. Essentially it’s composition under the hood, but
encapsulated inside the ORM model. The relational world sees it as a relation, and the OO
world sees it as inheritance.
Querying the MTI
                      superclass
While its not possible to run a query directly against an ABC, it is possible to run one against
an MTI superclass. But what if the superclass is directly tied to a record in the subclass table?

The Django ORM will return an object of the superclass model. The associated subclass
model is accessible as an attribute of the superclass model. So in this case “fred the user” is
accessible as the “user” attribute of “fred the person”.

This is a bit like composition again, but “fred the user” will inherit all the attributes of “fred
the person”, so it is better than where we were before.

If there is no corresponding user object for “fred the person”, accessing the user attribute will
raise an exception.
Discussion

More Related Content

What's hot

Object Oriended Programming with Java
Object Oriended Programming with JavaObject Oriended Programming with Java
Object Oriended Programming with Java
Jakir Hossain
 
03 Object Relational Mapping
03 Object Relational Mapping03 Object Relational Mapping
03 Object Relational Mapping
Ranjan Kumar
 
Hibernate Training Session1
Hibernate Training Session1Hibernate Training Session1
Hibernate Training Session1
Asad Khan
 

What's hot (20)

Object Oriended Programming with Java
Object Oriended Programming with JavaObject Oriended Programming with Java
Object Oriended Programming with Java
 
Introduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examplesIntroduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examples
 
03 Object Relational Mapping
03 Object Relational Mapping03 Object Relational Mapping
03 Object Relational Mapping
 
Inheritance Mixins & Traits
Inheritance Mixins & TraitsInheritance Mixins & Traits
Inheritance Mixins & Traits
 
Intro To Hibernate
Intro To HibernateIntro To Hibernate
Intro To Hibernate
 
javaopps concepts
javaopps conceptsjavaopps concepts
javaopps concepts
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and Interfaces
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
 
Lecture 8 Library classes
Lecture 8 Library classesLecture 8 Library classes
Lecture 8 Library classes
 
Classes2
Classes2Classes2
Classes2
 
Object oriented programming in php 5
Object oriented programming in php 5Object oriented programming in php 5
Object oriented programming in php 5
 
Core java notes with examples
Core java notes with examplesCore java notes with examples
Core java notes with examples
 
7494608
74946087494608
7494608
 
Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad
Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad
Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad
 
Ruby object model
Ruby object modelRuby object model
Ruby object model
 
Hibernate Training Session1
Hibernate Training Session1Hibernate Training Session1
Hibernate Training Session1
 
2014 Pre-MSc-IS-3 Persistence Layer
2014 Pre-MSc-IS-3 Persistence Layer2014 Pre-MSc-IS-3 Persistence Layer
2014 Pre-MSc-IS-3 Persistence Layer
 
Object oriented design-UNIT V
Object oriented design-UNIT VObject oriented design-UNIT V
Object oriented design-UNIT V
 
Java oops and fundamentals
Java oops and fundamentalsJava oops and fundamentals
Java oops and fundamentals
 

Similar to Model Inheritance

Intro Java Rev010
Intro Java Rev010Intro Java Rev010
Intro Java Rev010
Rich Helton
 
Object relationship mapping and hibernate
Object relationship mapping and hibernateObject relationship mapping and hibernate
Object relationship mapping and hibernate
Joe Jacob
 
Object Oriented Approach Within Siebel Boundaries
Object Oriented Approach Within Siebel BoundariesObject Oriented Approach Within Siebel Boundaries
Object Oriented Approach Within Siebel Boundaries
Roman Agaev
 
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
AnkurSingh340457
 
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
Gaurav Tyagi
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
Maryo Manjaruni
 

Similar to Model Inheritance (20)

JAVA-PPT'S.pdf
JAVA-PPT'S.pdfJAVA-PPT'S.pdf
JAVA-PPT'S.pdf
 
Intro Java Rev010
Intro Java Rev010Intro Java Rev010
Intro Java Rev010
 
Interview preparation for programming.pptx
Interview preparation for programming.pptxInterview preparation for programming.pptx
Interview preparation for programming.pptx
 
Object relationship mapping and hibernate
Object relationship mapping and hibernateObject relationship mapping and hibernate
Object relationship mapping and hibernate
 
Java interview questions and answers
Java interview questions and answersJava interview questions and answers
Java interview questions and answers
 
Object Oriented Approach Within Siebel Boundaries
Object Oriented Approach Within Siebel BoundariesObject Oriented Approach Within Siebel Boundaries
Object Oriented Approach Within Siebel Boundaries
 
Oop basic concepts
Oop basic conceptsOop basic concepts
Oop basic concepts
 
Unit 1 Java
Unit 1 JavaUnit 1 Java
Unit 1 Java
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
 
Cble assignment powerpoint activity for moodle 1
Cble assignment powerpoint activity for moodle 1Cble assignment powerpoint activity for moodle 1
Cble assignment powerpoint activity for moodle 1
 
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
 
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
 
JAVA-PPT'S.pptx
JAVA-PPT'S.pptxJAVA-PPT'S.pptx
JAVA-PPT'S.pptx
 
JAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptxJAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptx
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
 
JavaScript Miller Columns
JavaScript Miller ColumnsJavaScript Miller Columns
JavaScript Miller Columns
 
Selenium Training .pptx
Selenium Training .pptxSelenium Training .pptx
Selenium Training .pptx
 
Java basics
Java basicsJava basics
Java basics
 
Jump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design Pattern
 
Jump Start To Ooad And Design Patterns
Jump Start To Ooad And Design PatternsJump Start To Ooad And Design Patterns
Jump Start To Ooad And Design Patterns
 

More from Loren Davie

More from Loren Davie (11)

Blue Shift Marketing Plan
Blue Shift Marketing PlanBlue Shift Marketing Plan
Blue Shift Marketing Plan
 
Client management workshop for tech
Client management workshop for techClient management workshop for tech
Client management workshop for tech
 
CAVE Language Presentation for AI Camp
CAVE Language Presentation for AI CampCAVE Language Presentation for AI Camp
CAVE Language Presentation for AI Camp
 
Conversational Architecture: World IA Day 2016 NYC
Conversational Architecture: World IA Day 2016 NYCConversational Architecture: World IA Day 2016 NYC
Conversational Architecture: World IA Day 2016 NYC
 
Conversational Architecture, CAVE Language, Data Stewardship
Conversational Architecture, CAVE Language, Data StewardshipConversational Architecture, CAVE Language, Data Stewardship
Conversational Architecture, CAVE Language, Data Stewardship
 
Axilent Tool Talk from Breaking Development 2012
Axilent Tool Talk from Breaking Development 2012Axilent Tool Talk from Breaking Development 2012
Axilent Tool Talk from Breaking Development 2012
 
Django Multi-DB in Anger
Django Multi-DB in AngerDjango Multi-DB in Anger
Django Multi-DB in Anger
 
Django Intro
Django IntroDjango Intro
Django Intro
 
Django Environment
Django EnvironmentDjango Environment
Django Environment
 
Newforms Admin 101
Newforms Admin 101Newforms Admin 101
Newforms Admin 101
 
App Engine
App EngineApp Engine
App Engine
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 

Model Inheritance

  • 1. Model Inheritance ...in Django This presentation is about a feature of the Django Object Relational Mapping layer called “Model Inheritance”.
  • 2. Impedance Mismatch First some background. The root of the problem that Object Relational Mapping (ORM) systems attempt to solve is a fundamental “Impedance Mismatch” between the object- oriented world inhabited by OO programming languages, such as Python, and the relational database world as defined by RDBMS’ such as PostgreSQL, Oracle and MySQL. These two worlds have very different ways of looking at how data should be organized.
  • 3. Object Oriented Values One of the main tools of the OO world is inheritance. In OO inheritance, a descendant class inherits the characteristics of its ancestor. This allows common functionality to be programmed into the ancestor class, and then “specialized” sub-classes can be created, extending the functionality of the ancestor, or superclass. In this example, a User and a Customer are different kinds of specialized extensions of a Person. Additionally, an E-Commerce User inherits from both User and Customer, and therefore has all of the traits of its superclasses. It is common to organize data in OO in various hierarchies of superclasses and subclasses.
  • 4. Relational Values In the world of relational databases, database tables are defined with “foreign keys” that relate them to each other. In this example, we see that each row in the User table has a person_id, which is a foreign key that relates to a record in the Person table. Data organization is done through relating records together, and then composing queries that pulls the necessary data from the database, following the relationships defined by these foreign keys.
  • 5. ORM Layers are both OO and Relational Because they are designed to bridge the different approach to managing data by the two worlds, ORMs are inherently both Object Oriented AND Relational. It is exactly what is advertised by their name: Object Relational Mapping.
  • 6. Building Inheritance into ORMs is difficult Trying to bring OO-style inheritance to ORMs is difficult, because the relational world doesn’t really support the concept. It opens a lot of questions of the best way to provide inheritance functionality to the programmer, in a way that isn’t a total hack on the relational side.
  • 7. Django approach before now: Use Composition Up until now, Django hasn’t really supported model inheritance. When a Django application developer was presented with a situation that would best be solved with inheritance, they were advised to use a technique called composition instead. The most prominent example of this is the user profile. It would be a common case for a programmer to want to extend the User class that comes in django.contrib.auth so that they can contain attributes specific to the application. Django historically has solved this with a “user profile” - a separate class that is identified in settings.py and can be retrieved by the get_user_profile() method on User objects.
  • 8. Unique Foreign Key To use composition, one defines a foreign key that is also unique on the composited class (the class that would be the subclass if we were solving this problem using OO inheritance). For example, if we hypothetically wanted to specialize a Person class and make a User, the User would have a ForeignKey field that pointed back at the Person model (the Django ORM equivalent of a relational foreign key). In addition, we would designate the field unique, ensuring that there would always only be one User per Person. Traits on the composited “superclass” would have to be accessed explicitly - they are not truly inherited by the specialization class.
  • 9. This Sucks There are a number of reasons this is a suboptimal situation. 1. It makes queries more complicated, meaning that the work of defining the relationship that should be done once in data modeling is now pushed all over the application code. 2. It fails to take advantage of Python’s OO features and thus, power. 3. It creates an object model that doesn’t really best describe the real-world entities that it is trying model.
  • 10. Malcolm Tredinnick This man has come to the rescue, however. This is Malcom Tredinnick, and he has for some time now been working on a branch of the Django code called QuerySet Refactor.
  • 11. Queryset Refactor The QuerySet Refactor branch brings a number of new features to the Django ORM. Most notably it has brought true Model Inheritance. Despite being difficult to program, the QuerySet Refactor programmers, led by Malcolm, have managed to implement it in a couple of great ways. Furthermore, on April 26th this year, QuerySet Refactor was merged into the Django Trunk. That means that all of its features are now part of the mainstream development of Django.
  • 12. Two Approaches to Model Inheritance With the merge complete, Django now offers two different approaches to Model Inheritance. An application might choose to use both approaches, since they offer advantages in different circumstances.
  • 13. 1. Abstract Base Classes The first approach is called Abstract Base Classes. This approach is best used when the parent superclass is never meant to be instantiated on its own. It is merely a source of common functionality that will be used by subclasses. Abstract Base Classes are specified by adding “abstract = True” in the Meta inner class. When syncdb is run, the Django ORM will not create a database table for these models. Model classes that extend the Abstract Base Class will automatically inherit fields defined in the superclass, however. The Django ORM will automatically generate corresponding columns and relations for the superclass’ fields in the table of the subclass.
  • 14. Abstract Base Classes are a coding convenience only. In the case of Abstract Base Classes, the inheritance relationship is ignored at the relational level. They are essentially a kind of advanced syntactic sugar - providing a type of “include” in model definitions. Once the Django ORM has “compiled” the models into SQL, the Abstract Base Class essentially ceases to exist.
  • 15. ABC Gotcha “related_name” Abstract Base Classes can carry a couple of gotchas. Let’s look at one related to the use of the “related_name” attribute in ForeignKey fields.
  • 16. Specifying a ForeignKey in an ABC superclass If we put a foreign key field in an ABC, we might wish to specify a related_name in a ForeignKey field. The related_name is the name by which this class (“Person”) is known by the target model of the ForeignKey (“Company”). So a Company object has “people”. The problem comes when we have more than one concrete subclass of the ABC. We have to remember that there is no database table that corresponds to Person. Instead, the Django ORM compiles the fields of the ABC into the table definitions of the subclasses. This means that Company objects will have a “people” field that points to both User and Customer objects. Django will throw an error when syncdb is run.
  • 17. Specifying a ForeignKey in an ABC superclass The suggested solution is to change the related name to something that is dynamically interpolated. By using the “%(class)s_related” notation, we will create two attributes in the Company model: “user_related” pointing to users, and “customer_related” pointing to customers.
  • 18. 2. Multiple Table Inheritance The second approach to Model Inheritance that Django now provides is called Multiple Table Inheritance. In this approach, Django generates a separate relational table for each model in the inheritance hierarchy. This approach is useful for circumstances where you may wish to instantiate models from both the superclass and its subclasses. In this example, we may wish to define Persons that are not Users, in addition to Users that inherit Person attributes. MTI inheritance can be engaged simply by subclassing an existing model. In this case there is no “abstract” attribute being set in an inner Meta class.
  • 19. MTI makes inheritance a relationship under the hood. As mentioned, when the Django ORM generates SQL from models using MTI, a separate table is created for both the superclasses and subclasses. In addition, the inheritance between models is converted into a relation. The subclass table will have a special additional column called “<model_name>_ptr_id”, a foreign key that points at the superclass table. When an instance of the subclass model is pulled from the database, the ORM will pull the related row in the superclass table and build a model instance with data from both tables. Essentially it’s composition under the hood, but encapsulated inside the ORM model. The relational world sees it as a relation, and the OO world sees it as inheritance.
  • 20. Querying the MTI superclass While its not possible to run a query directly against an ABC, it is possible to run one against an MTI superclass. But what if the superclass is directly tied to a record in the subclass table? The Django ORM will return an object of the superclass model. The associated subclass model is accessible as an attribute of the superclass model. So in this case “fred the user” is accessible as the “user” attribute of “fred the person”. This is a bit like composition again, but “fred the user” will inherit all the attributes of “fred the person”, so it is better than where we were before. If there is no corresponding user object for “fred the person”, accessing the user attribute will raise an exception.