SlideShare une entreprise Scribd logo
1  sur  21
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

Object Oriended Programming with Java
Object Oriended Programming with JavaObject Oriended Programming with Java
Object Oriended Programming with JavaJakir Hossain
 
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 examplesecosio GmbH
 
03 Object Relational Mapping
03 Object Relational Mapping03 Object Relational Mapping
03 Object Relational MappingRanjan Kumar
 
Intro To Hibernate
Intro To HibernateIntro To Hibernate
Intro To HibernateAmit Himani
 
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 InterfacesAhmed Nobi
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in phpCPD INDIA
 
Object oriented programming in php 5
Object oriented programming in php 5Object oriented programming in php 5
Object oriented programming in php 5Sayed Ahmed
 
Core java notes with examples
Core java notes with examplesCore java notes with examples
Core java notes with examplesbindur87
 
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 NicheTech Com. Solutions Pvt. Ltd.
 
Hibernate Training Session1
Hibernate Training Session1Hibernate Training Session1
Hibernate Training Session1Asad Khan
 
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 Layerandreasmartin
 
Object oriented design-UNIT V
Object oriented design-UNIT VObject oriented design-UNIT V
Object oriented design-UNIT VAzhar Shaik
 
Java oops and fundamentals
Java oops and fundamentalsJava oops and fundamentals
Java oops and fundamentalsjavaease
 

Tendances (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
 

Similaire à Model Inheritance

Intro Java Rev010
Intro Java Rev010Intro Java Rev010
Intro Java Rev010Rich Helton
 
Interview preparation for programming.pptx
Interview preparation for programming.pptxInterview preparation for programming.pptx
Interview preparation for programming.pptxBilalHussainShah5
 
Object relationship mapping and hibernate
Object relationship mapping and hibernateObject relationship mapping and hibernate
Object relationship mapping and hibernateJoe Jacob
 
Java interview questions and answers
Java interview questions and answersJava interview questions and answers
Java interview questions and answersKrishnaov
 
Object Oriented Approach Within Siebel Boundaries
Object Oriented Approach Within Siebel BoundariesObject Oriented Approach Within Siebel Boundaries
Object Oriented Approach Within Siebel BoundariesRoman Agaev
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPTAjay Chimmani
 
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 1LK394
 
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 PresentationGaurav Tyagi
 
JAVA-PPT'S.pptx
JAVA-PPT'S.pptxJAVA-PPT'S.pptx
JAVA-PPT'S.pptxRaazIndia
 
JAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptxJAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptxKunalYadav65140
 
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 conceptsMaryo Manjaruni
 
JavaScript Miller Columns
JavaScript Miller ColumnsJavaScript Miller Columns
JavaScript Miller ColumnsJonathan Fine
 
Selenium Training .pptx
Selenium Training .pptxSelenium Training .pptx
Selenium Training .pptxSajidTk2
 
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 PatternNishith Shukla
 
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 PatternsLalit Kale
 

Similaire à 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
 

Plus de Loren Davie

Blue Shift Marketing Plan
Blue Shift Marketing PlanBlue Shift Marketing Plan
Blue Shift Marketing PlanLoren Davie
 
Client management workshop for tech
Client management workshop for techClient management workshop for tech
Client management workshop for techLoren Davie
 
CAVE Language Presentation for AI Camp
CAVE Language Presentation for AI CampCAVE Language Presentation for AI Camp
CAVE Language Presentation for AI CampLoren Davie
 
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 NYCLoren Davie
 
Conversational Architecture, CAVE Language, Data Stewardship
Conversational Architecture, CAVE Language, Data StewardshipConversational Architecture, CAVE Language, Data Stewardship
Conversational Architecture, CAVE Language, Data StewardshipLoren Davie
 
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 2012Loren Davie
 
Django Multi-DB in Anger
Django Multi-DB in AngerDjango Multi-DB in Anger
Django Multi-DB in AngerLoren Davie
 
Django Environment
Django EnvironmentDjango Environment
Django EnvironmentLoren Davie
 
Newforms Admin 101
Newforms Admin 101Newforms Admin 101
Newforms Admin 101Loren Davie
 

Plus de 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
 

Dernier

SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 

Dernier (20)

SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 

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.